Scala – Tuples & Lists

A Tuple is a generalization of a collection of two, three, four, and more values. Each value can have its own type.

scala> val twoInts = (5,10)
	twoInts: (Int, Int) = (5,10)
scala> val twoStrings = ("Hello", "World")
	twoStrings: (String, String) = (Hello,World)
scala> val threeDoubles = (1.11, 2.22, 3.33)
	threeDoubles: (Double, Double, Double) = (1.11,2.22,3.33)
scala> val intAndString = (1, "Good Evening")
	intAndString: (Int, String) = (1,Good Evening)
scala> val mixedUp = (1, "Vinay Chauhan", 90.50)
	mixedUp: (Int, String, Double) = (1,Vinay Chauhan,90.5)

The elements of a Tuple can be recovered in a few different ways. One way is to use a Tuple when initializing some variables, each of which takes on the value of the corresponding position in the Tuple on the right side of the equal sign.

scala> var (firstInt,secondInt) = twoInts
	firstInt: Int = 5
	secondInt: Int = 10

scala> firstInt
	res39: Int = 5	

Similarly, we could do the actvity for other tuples. 

scala> var (rollNo,name,marks) = mixedUp
	rollNo: Int = 1
	name: String = Vinay Chauhan
	marks: Double = 90.5

scala> rollNo
	res41: Int = 1

scala> name
	res42: String = Vinay Chauhan

scala> marks
	res43: Double = 90.5

Few Tips
Another way to access the values in a Tuple is via indexation, using “_n” where n is the index of the item you want.

scala> print(mixedUp._1)
scala> print(mixedUp._2)
	Vipin Chauhan
scala> print(mixedUp._3)

Few Tips
An alternate form of creating a 2-sized tuple is with the relation operator (->). This is a popular shortcut for representing key-value pairs in tuples:	

scala> val fruitData = "FruitKing" -> "Mango"
	fruitData: (String, String) = (FruitKing,Mango)

scala> fruitData._1
	res3: String = FruitKing

scala> fruitData._2
	res4: String = Mango

Lists are collections of ordered items that will be familiar to anyone who has done any shopping. Tuples are obviously related to lists, but they are less versatile in that they must be created in a single statement, they have a bounded length (about 20 or so), and they don’t support operations that perform computations on all of their elements. In Scala, we can create lists of Strings, Ints, and Doubles (and more).

scala> val fruitList = List("Mango","Orange","PineApple")
	fruitList: List[String] = List(Mango, Orange, PineApple))

scala> fruitList.head
	res0: String = Mango

scala> fruitList.tail
	res2: List[String] = List(Orange, PineApple)

scala> fruitList
	res3: List[String] = List(Mango, Orange, PineApple)

scala> fruitList(1)
	res4: String = Orange

scala> fruitList(2)
	res5: String = PineApple

scala> for(data <- fruitList) println(data) Mango Orange PineApple scala> val oddList = List(1,3,5,7);
	oddList: List[Int] = List(1, 3, 5, 7)

We see that Scala responds that a List has been created, along with brackets around the type of the elements it contains. So, List[Int] is read as “a List of Ints” and so on. This is to say that List is a parameterized data structure: it is a container that holds elements of specific types. 

Few Tips
We can also create Lists with mixtures of types.

scala> val intsAndDoubles  = List (1,1.5,2,2.5)
	intsAndDoubles: List[Double] = List(1.0, 1.5, 2.0, 2.5)

scala> val embeddedList = (fruitList,oddList);
	embeddedList: (List[String], List[Int]) = (List(Mango, Orange, PineApple),List(1, 3, 5, 7))

Use Case for Reducer 
scala> val numbers = List(1,2,3,4,5)
	numbers: List[Int] = List(1, 2, 3, 4, 5)

scala> val total = numbers.reduce((a:Int, b:Int) => a+b)
	total: Int = 15

List methods
scala> fruitList.length
	res48: Int = 3

Comments: The notation variable.method indicates that you are invoking a function that is specific to the type of that variable on the value in that variable. Scala is an object-oriented language, which means that every value has a set of actions that comes with it. Which actions are available depends on its type. 

scala> 2.+(3)
	res49: Int = 5

scala> "Good".+("Evening")
	res51: String = GoodEvening	

Comments: Lets return to Lists and what we can do with them. “Addition” of two lists is their concatenation and is indicated with “++“.

scala> val oddList = List(1,3,5,7);
	oddList: List[Int] = List(1, 3, 5, 7)

scala> val evenList = List (2,4,6,8);
	evenList: List[Int] = List(2, 4, 6, 8)

scala> val completeList = oddList ++ evenList
	completeList: List[Int] = List(1, 3, 5, 7, 2, 4, 6, 8)

Comments: We can append a single item to the front of a List with “::“.

scala> val completeList = 0 :: oddList ++ evenList
	completeList: List[Int] = List(0, 1, 3, 5, 7, 2, 4, 6, 8)

And sort a list with sorted, and reverse it with reverse, and do both in sequence

scala> completeList.sorted
	res52: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)

scala> completeList.reverse
	res54: List[Int] = List(8, 6, 4, 2, 7, 5, 3, 1, 0)

scala> completeList.sorted.reverse
	res55: List[Int] = List(8, 7, 6, 5, 4, 3, 2, 1, 0)

Note: List is immutable: you cannot change it, so all of these operations will always return new Lists. 

scala> val myList = completeList.sorted
	myList: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8)

scala> myList(2);
	res57: Int = 2

Another useful method is slice, which gives you a sublist from one index up to, but not including, another.
scala> myList.slice(2,6)
	res58: List[Int] = List(2, 3, 4, 5)

Therefore, the slice gave us a list with the elements from index 2 (the third element) up to index 5 (the sixth element).

Few Tips
On lists that contain numbers, we can use the sum method.
scala> myList.sum
	res60: Int = 36

One thing we often want to do with lists is obtain a String representation of their contents in some visually useful way. For example, we might want a fruitList to be a String with one item per line, or a list of Ints to have a comma between each element. The mkString method does just what we need.

scala> fruitList.mkString("\n")
	res61: String =

Want to know if a list contains a particular element? Use contains on the list.
scala> fruitList.contains("Apple")
	res63: Boolean = false

scala> fruitList.contains("Mango")
	res64: Boolean = true

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s