Scala Quick Notes :: Part - 2


Bhaskar S 01/10/2015


Overview

In this part of the series, we will look at Scala collections such as List, Set, Map, and Tuple.

By default, we get an immutable version of these Scala collections (unlike in Java) staying true to the functional style of programming. If we desire the mutable version (like Java), we will have to explicitly refer to the package scala.collection.mutable.

Hands-on With Scala - II

The following is the Scala program named Sample04.scala:

Sample04.scala
/*
 *
 * Name  : Sample04
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/10/2015
 *  
 */

package com.polarsparc.scala

object Sample04 {
  def main(args: Array[String]) = {
    val zippo = List ()
    
    val brands = List[String] ("BMW", "Cadillac", "Honda", "Lexus", "Mazda")
    val moreBrands = List[String] ("Nissan", "Toyota")
    
    val nums = 1 :: 2 :: 3 :: 4 :: 5 :: Nil
    
    val evenNums = List.range(2, 11, 2)
    val oddNums = List.range(1, 11, 2)
    
    printf("Size of zippo - %d\n", zippo.size)
    printf("Is zippo empty - %s\n", zippo.isEmpty)
    
    printf("Size of brands - %d\n", brands.size)
    printf("Head item of brands - %s\n", brands.head)
    printf("Last item of brands - %s\n", brands.last)
    printf("Tail of brands - %s\n", brands.tail)
    printf("3rd item of brands - %s\n", brands(2))
    printf("Reverse of brands - %s\n", brands.reverse)
    printf("Items of brands - %s\n", brands)
    printf("Nissan appended to brands - %s\n", brands :+ "Nissan")
    printf("Audi prepended to brands - %s\n", "Audi" +: brands)
    printf("Audi prepended to brands (using ::) - %s\n", "Audi" :: brands)
    
    printf("Items in nums: ")
    for (n <- nums) {
      printf("%d ", n)
    }
    printf("\n")
    
    printf("Drop first 2 items of nums - %s\n", nums.drop(2))
    printf("Drop lasst 2 items of nums - %s\n", nums.dropRight(2))
    
    printf("Items of evenNums - %s\n", evenNums)
    printf("Items of oddNums - %s\n", oddNums)
    printf("Items of evenNums and oddNums - %s\n", evenNums ++ oddNums)
    printf("Items of evenNums and oddNums (using :::) - %s\n", evenNums ::: oddNums)
  }
}

Executing the program Sample04.scala results in the output:

Output (Sample04.scala)

Size of zippo - 0
Is zippo empty - true
Size of brands - 5
Head item of brands - BMW
Last item of brands - Mazda
Tail of brands - List(Cadillac, Honda, Lexus, Mazda)
3rd item of brands - Honda
Reverse of brands - List(Mazda, Lexus, Honda, Cadillac, BMW)
Items of brands - List(BMW, Cadillac, Honda, Lexus, Mazda)
Nissan appended to brands - List(BMW, Cadillac, Honda, Lexus, Mazda, Nissan)
Audi prepended to brands - List(Audi, BMW, Cadillac, Honda, Lexus, Mazda)
Audi prepended to brands (using ::) - List(Audi, BMW, Cadillac, Honda, Lexus, Mazda)
Items in nums: 1 2 3 4 5 
Drop first 2 items of nums - List(3, 4, 5)
Drop lasst 2 items of nums - List(1, 2, 3)
Items of evenNums - List(2, 4, 6, 8, 10)
Items of oddNums - List(1, 3, 5, 7, 9)
Items of evenNums and oddNums - List(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
Items of evenNums and oddNums (using :::) - List(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)

The following section explains some of the aspects of the Scala program Sample04.scala:

The following is the Scala program named Sample05.scala:

Sample05.scala
/*
 *
 * Name  : Sample05
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/10/2015
 *  
 */

package com.polarsparc.scala

object Sample05 {
  def main(args: Array[String]) = {
    val zippo = Set ()
    
    val brands = Set[String] ("BMW", "Cadillac", "Honda", "Lexus", "Mazda")
    val moreBrands = Set[String] ("Lexus", "Mazda", "Nissan", "Toyota")
    
    printf("Size of zippo - %d\n", zippo.size)
    printf("Is zippo empty - %s\n", zippo.isEmpty)
    
    printf("Size of brands - %d\n", brands.size)
    printf("Head item of brands - %s\n", brands.head)
    printf("Last item of brands - %s\n", brands.last)
    printf("Tail of brands - %s\n", brands.tail)
    printf("Contains Audi in brands - %s\n", brands.contains("Audi"))
    printf("Contains Lexus in brands - %s\n", brands.contains("Lexus"))
    printf("Items of brands - %s\n", brands)
    printf("Nissan added to brands - %s\n", brands + "Nissan")
    printf("Lexus removed from brands - %s\n", brands - "Lexus")
    
    printf("Items in moreBrands: %s\n", moreBrands.mkString(", "))
    
    printf("Drop first 2 items of moreBrands - %s\n", moreBrands.drop(2))
    printf("Drop lasst 2 items of moreBrands - %s\n", moreBrands.dropRight(2))
    
    printf("Items of brands and moreBrands - %s\n", brands ++ moreBrands)
    printf("Items of moreBrands removed from brands - %s\n", brands -- moreBrands)
    printf("Items common to brands and moreBrands - %s\n", brands & moreBrands)
  }
}

Executing the program Sample05.scala results in the output:

Output (Sample05.scala)

Size of zippo - 0
Is zippo empty - true
Size of brands - 5
Head item of brands - Honda
Last item of brands - Mazda
Tail of brands - Set(Lexus, Cadillac, BMW, Mazda)
Contains Audi in brands - false
Contains Lexus in brands - true
Items of brands - Set(Honda, Lexus, Cadillac, BMW, Mazda)
Nissan added to brands - Set(Honda, Lexus, Cadillac, BMW, Nissan, Mazda)
Lexus removed from brands - Set(Honda, Cadillac, BMW, Mazda)
Items in moreBrands: Lexus, Mazda, Nissan, Toyota
Drop first 2 items of moreBrands - Set(Nissan, Toyota)
Drop lasst 2 items of moreBrands - Set(Lexus, Mazda)
Items of brands and moreBrands - Set(Toyota, Honda, Lexus, Cadillac, BMW, Nissan, Mazda)
Items of moreBrands removed from brands - Set(Honda, Cadillac, BMW)
Items common to brands and moreBrands - Set(Lexus, Mazda)

The following section explains some of the aspects of the Scala program Sample05.scala:

The following is the Scala program named Sample06.scala:

Sample06.scala
/*
 *
 * Name  : Sample06
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/10/2015
 *  
 */

package com.polarsparc.scala

object Sample06 {
  def main(args: Array[String]) = {
    val zippo = Map[Int, String] ()
    
    val oneToThree = Map[Int, String] (1 -> "One", 2 -> "Two", 3 -> "Three")
    val ThreeToSix = Map[Int, String] (3 -> "Three", 4 -> "Four", 5 -> "Five", 6 -> "Six")
    
    printf("Size of zippo - %d\n", zippo.size)
    printf("Is zippo empty - %s\n", zippo.isEmpty)
    
    printf("Size of oneToThree - %d\n", oneToThree.size)
    printf("Size of ThreeToSix - %d\n", ThreeToSix.size)
    printf("Head item of oneToThree - %s\n", oneToThree.head)
    printf("Last item of oneToThree - %s\n", oneToThree.last)
    printf("Tail of oneToThree - %s\n", oneToThree.tail)
    printf("Contains 0 in oneToThree - %s\n", oneToThree.contains(0))
    printf("Contains 3 in oneToThree - %s\n", oneToThree.contains(3))
    printf("Get 0 from oneToThree - %s\n", oneToThree.get(0))
    printf("Get 3 from oneToThree - %s\n", oneToThree.get(3))
    printf("Items of oneToThree - %s\n", oneToThree)
    printf("Keys of oneToThree - %s\n", oneToThree.keys)
    printf("Zero added to oneToThree - %s\n", oneToThree + (0 -> "Zero"))
    printf("Zero removed from oneToThree - %s\n", oneToThree - 0)
    
    printf("Items in ThreeToSix: %s\n", ThreeToSix.mkString(", "))
    
    printf("Drop first 2 items of ThreeToSix - %s\n", ThreeToSix.drop(2))
    printf("Drop lasst 2 items of ThreeToSix - %s\n", ThreeToSix.dropRight(2))
    
    printf("Items of oneToThree and ThreeToSix - %s\n", oneToThree ++ ThreeToSix)
    printf("Items 5 and 6 removed from ThreeToSix - %s\n", ThreeToSix -- List(5, 6))
  }
}

Executing the program Sample06.scala results in the output:

Output (Sample06.scala)

Size of zippo - 0
Is zippo empty - true
Size of oneToThree - 3
Size of ThreeToSix - 4
Head item of oneToThree - (1,One)
Last item of oneToThree - (3,Three)
Tail of oneToThree - Map(2 -> Two, 3 -> Three)
Contains 0 in oneToThree - false
Contains 3 in oneToThree - true
Get 0 from oneToThree - None
Get 3 from oneToThree - Some(Three)
Items of oneToThree - Map(1 -> One, 2 -> Two, 3 -> Three)
Keys of oneToThree - Set(1, 2, 3)
Zero added to oneToThree - Map(1 -> One, 2 -> Two, 3 -> Three, 0 -> Zero)
Zero removed from oneToThree - Map(1 -> One, 2 -> Two, 3 -> Three)
Items in ThreeToSix: 3 -> Three, 4 -> Four, 5 -> Five, 6 -> Six
Drop first 2 items of ThreeToSix - Map(5 -> Five, 6 -> Six)
Drop lasst 2 items of ThreeToSix - Map(3 -> Three, 4 -> Four)
Items of oneToThree and ThreeToSix - Map(5 -> Five, 1 -> One, 6 -> Six, 2 -> Two, 3 -> Three, 4 -> Four)
Items 5 and 6 removed from ThreeToSix - Map(3 -> Three, 4 -> Four)

The following section explains some of the aspects of the Scala program Sample06.scala:

The following is the Scala program named Sample07.scala:

Sample07.scala
/*
 *
 * Name  : Sample07
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/10/2015
 *  
 */

package com.polarsparc.scala

import scala.util._

object Sample07 {
  def main(args: Array[String]) = {
    val random: Random = new Random
    
    def waitTimeByUser(name: String) = {
      (name, random.nextInt(10))
    }
    
    val contact = ("Alice", "alice@earth.com")
    
    val userWait = waitTimeByUser("Alice")
    
    printf("Tuple contact - %s\n", contact)
    printf("First element of contact - %s\n", contact._1)
    printf("Second element of contact - %s\n", contact._2)
    
    printf("Tuple userWait - %s\n", userWait)
    printf("First element of userWait - %s\n", userWait._1)
    printf("Second element of userWait - %s\n", userWait._2)
  }
}

Executing the program Sample07.scala results in the output:

Output (Sample07.scala)

Tuple contact - (Alice,alice@earth.com)
First element of contact - Alice
Second element of contact - alice@earth.com
Tuple userWait - (Alice,4)
First element of userWait - Alice
Second element of userWait - 4

The following section explains some of the aspects of the Scala program Sample07.scala:

References

Scala Quick Notes :: Part - 1