Some strong “building-blocks” of scala (Tutorial 1 : Lists)


Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Scala supports functional programming & is a pure object-oriented language in the sense that every value is an object. Let us discuss about some of the strongest building blocks of Scala.

Scala Lists:
Lists are an important data structure in many Scala programs. A list containing the elements x1, . . . , xn is written List(x1, …, xn).Lets have an example of List and some operations on it:

package tester

object DemoList {
  var DemoList1 = List(1, 3, 2, 5, 4)
  var DemoList2 = List("Neelkanth", "Sachdeva", "Knoldus")
  var sum: Int = 0

  def main(args: Array[String]) {

    val CombinedElemList = DemoList1 ::: DemoList2
    /*Combines the elements of both the list in to one List*/
    println(CombinedElemList)

    DemoList1 foreach (sum += _)
    /*Sum all the elements in List*/
    println("Sum of all the elements in DemoList1 is"+sum)

    val flatMappedList = DemoList2.flatMap(_.toList)
    /*split all the characters in each string of list
    * & arrange them in to a single List */
    println(flatMappedList)

    /*
     * Finding the first and last element of List
     */
    val firstElementinDemoList1 = DemoList1.head
    val lastElementinDemoList1 = DemoList1.last
    val myLength=DemoList1.length
    println("My First Element is " + firstElementinDemoList1 
    +" My Last Element is " + lastElementinDemoList1 + " My Length is " + myLength)
    
    /*
     * Adding and Removing a element from List
     */
     
    val listAfterDeletingOneElements= DemoList2 -- List("Knoldus")
    println("List after removing one element"+listAfterDeletingOneElements)
    
    val listAfterAddingElements= listAfterDeletingOneElements ++ List("Knoldus" , " Software")
    println("List after adding two elements"+listAfterAddingElements)
  }

}

Result for the code above would look like:

With these special operations shown in this example, Lists has a lot of other convenient operations for use like below.

 /*
     * Filtering the Lists
     */

    val fixedLengthList = DemoList2 filter (DemoList1 => DemoList1.length == 7)
    // This will print the List of all elements in DemoList1 that has a length 7
    println("List of words containing 7 character" + fixedLengthList)

    val listContainingtheRequiredCharacter = DemoList2 filter (DemoList1 => DemoList1.contains("ach"))
    // This will print the List of all elements that contains the desired character sequence
    println("List of words having desired character sequence " + listContainingtheRequiredCharacter)

    /*
     * Reversing the List
     */
    val reversedList = DemoList2.reverse
    println("Reversing the order of List " + reversedList)

    // Dropping the element from List
    val listAfterDroppingElement = DemoList2.drop(1)
    println("List after dropping element from the specified index " + listAfterDroppingElement)

    //Splitting the List
    val listAfterSplittingatSpecifiedIndex = DemoList2.splitAt(1)
    println("The Splitted List are Here " + listAfterSplittingatSpecifiedIndex)

    //Slicing the List
    val listAfterSlicing = DemoList2.slice(1, 3)
    println("Sliced List According to specified limit" + listAfterSlicing)

    var DemoList3 = List("America", "India", "Canada", "India", "Canada")

    // Removing Duplicates
    val listAfterRemovingDuplicates = DemoList3.removeDuplicates
    println("List After Removing Duplicates is " + listAfterRemovingDuplicates)

    // Taking Desired No of elements in a List 
    val listOfDesiredNoOfElements = DemoList3.take(2)
    println("List of Desired No of Elements" + listOfDesiredNoOfElements)

    // Generating List Before a specified element 
    val listBeforeASpecifiedElement = DemoList2.takeWhile (_ > "Knoldus")
    println("List Before a Specified Element " + listBeforeASpecifiedElement)
    
    // Generating List after Dropping to a specified element 
    val listAfteraDropLimit = DemoList2 dropWhile (_ > "Knoldus")
    println("List After Dropping to a Specified Element " + listAfteraDropLimit)

    //Partitioning the List
    val listAfterPartition=DemoList2 partition (_ > "Knoldus")
    println("List after Partitioning is " + listAfterPartition)
    
   val breakingListAfterSpecifiedElement=DemoList2.break(_ > "Neelkanth")
   println("List after breaking from certain point"+ breakingListAfterSpecifiedElement) 
  }

Result for the code above would look like:

About these ads

About Neelkanth Sachdeva

Software Consultant @ Knoldus Software LLP Co-Founder @ My Cell Was Stolen
This entry was posted in Web. Bookmark the permalink.

One Response to Some strong “building-blocks” of scala (Tutorial 1 : Lists)

  1. More

    Adding to the head of the list

    scala> var l = List(1, 2, 3)
    l: List[Int] = List(1, 2, 3)

    scala> l ::= 4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s