Hello readers, welcome to my blog !!!
In this blog, we will see how to use lists in Scala and higher order functions that can be performed on lists.
Lists in Scala is the most widely used Scala collection. There is both mutable and immutable version of the list. However, we will be using an immutable version of lists.
Lists are quite similar to arrays, like arrays, lists are homogeneous: the elements of a list all have the same type but there are two important differences. First, lists are by default immutable. That is, elements of a list cannot be changed by assignment. Second, lists have a recursive structure (i.e., a linked list), whereas arrays are flat.
Let’s see how to construct lists in Scala.
All lists are built from two fundamental building blocks i.e. Nil and :: (pronounced “cons“). Nil represents the empty list. The infix operator, ::, is used to extend list elements. That is, 1 :: 2 :: Nil represents a list whose first element is 1, followed by element 2.
You can construct Scala list by following two ways
val nums = 1 :: 2 :: 3 :: 4 :: 5 :: Nil
val nums = List(1, 2, 3, 4, 5)
we can create an empty list by
val emptyList = Nil
val emptyList= List()
Basic List Operation
All operations on lists can be expressed in terms of the following three:
- head – returns the first element of a list.
- tail – returns a list consisting of all elements except the first.
- isEmpty – returns true if the list is empty.
scala>val triplets = List("abc", "def", "ghi", "jkl") triplets: List[String] = List(abc, def, ghi, jkl) scala> triplets.head res0: String = abc scala> triplets.tail res1: List[String] = List(def, ghi, jkl) scala> triplets.isEmpty res2: Boolean = false
First-Order Methods on Class List
- length – computes the length of a list.
scala> triplets.length res3: Int = 4
- init – returns a list consisting of all elements except the last one
scala> triplets.init res4: List[String] = List(abc, def, ghi)
- last – returns the last element of a (non-empty) list.
scala> triplets.last res5: String = jkl
- reverse – reverses the list.
scala> triplets.reverse res6: List[String] = List(jkl, ghi, def, abc)
- drop – “xs take n” returns the first n elements of the list xs.
scala> triplets drop 2 res7: List[String] = List(ghi, jkl)
- take – “xs take n” returns the first n elements of the list xs
scala> triplets take 2 res8: List[String] = List(abc, def)
Note: If n is greater than xs.length, the whole list xs is returned.
- splitAt – splits the list at a given index, returning a pair of two lists.
scala> triplets splitAt 2 res9: (List[String], List[String]) = (List(abc, def),List(ghi, jkl))
- apply – “xs apply n” or “xs(n) Random element selection is supported through the apply method; however it is a less common operation for lists than it is for arrays.
scala> triplets apply 3 res10: String = jkl
Note: random element selection is less popular for lists than for arrays is that xs(n)takes time proportional to the index n.
- indices – returns a list consisting of all valid indices of a given list.
scala> triplets.indices res11: scala.collection.immutable.Range = Range 0 until 4
- flatten – takes a list of lists and flattens it out to a single list.
scala> List(triplets) res12: List[List[String]] = List(List(abc, def, ghi, jkl)) scala> res12.flatten res13: List[String] = List(abc, def),List(ghi, jkl)
- zip – zip operation takes two lists and forms a list of pairs
scala> val nums = List(1, 2, 3, 4, 5) nums: List[Int] = List(1, 2, 3, 4, 5) scala> val zip1 = nums zip triplets zip1: List[(Int, String)] = List((1,abc), (2,def), (3,ghi), (4,jkl))
- unzip – changes list of tuples back to a tuple of lists that were zipped.
val unzip1 = zip1.unzip unzip1: (List[Int], List[String]) = (List(1, 2, 3, 4),List(abc, def, ghi, jkl))
- zipWithIndex – pairs every element of a list with the position where it appears in the list.
triplets.zipWithIndex res14: List[(String, Int)] = List((abc,0), (def,1), (ghi,2), (jkl,3))
- toString – returns the canonical string representation of a list.
scala> triplets.toString res15: String = List(abc, def, ghi, jkl)
- mkString – used for different representation and it involves four operands i.e. mkString (pre, sep, post)
- the list xs to be displayed
- a prefix string pre to be displayed in front of all elements
- a separator string sep to be displayed between successive elements
- a postfix string post to be displayed at the end.
scala> triplets.mkString("abc"," ","jkl") res16: String = abcabc def ghi jkljkl
- iterator – access list elements via an iterator.
scala> triplets.iterator res17: Iterator[String] = <iterator> scala> res12.next res17: String = abc scala> res12.next res18: String = def
- toList – converts data to a list.
scala> triplets.toArray res19: Array[String] = Array(abc, def, ghi, jkl) scala> res15.toList res20: List[String] = List(abc, def, ghi, jkl) scala> "abc".toList res21: List[Char] = List(a, b, c)
- copyToArray – copies list elements to successive array positions within some destination array.
scala> val arr=new Array[String](5) arr: Array[String] = Array(null, null, null, null, null) scala> res15.copyToArray(arr,1) scala> arr res21: Array[String] = Array(null, abc, def, ghi, jkl)
These operations are defined as methods of class List.
Conclusion: In this blog, we learned about lists in Scala. We went through creating lists in Scala and first-order methods on class List.
With that I would conclude this blog, I hope that this was informative and easy to understand, also, if you have any queries please feel free to list them down in the comments section.