Thursday 28 December 2017

Introduction to Scala - XIII

We continue with more Scala on the topic of Vectors in this post. For all the work in this post, we will use Read-Evaluate-Print-Loop Scala interpreter. Some familiarity with Java will be of great help in understanding Scala. In this post, we will look at how Vectors are defined along with some features of Vectors in Scala.

After looking at Arrays in last post, we will see how Vector are useful in Scala programs. Vector is a immutable data structure that provides random access and updates in effectively constant time. It also facilitates very fast append and prepend.

Vectors can be defined as shown below:

i) An empty Vector:

val Vector0: Vector[Int] = Vector()

The results are shown below:

scala> val Vector0: Vector[Int] = Vector()
Vector0: Vector[Int] = Vector()

scala> Vector0
res7: Vector[Int] = Vector()


ii) A Vector initialized with values:

val Vector1 = Vector(1,1,2,3,4,5)
val Vector2 = Vector(5,6,7,8,9,10,10)

The results are shown below:

scala> val Vector1 = Vector(1,1,2,3,4,5)
Vector1: scala.collection.immutable.Vector[Int] = Vector(1, 1, 2, 3, 4, 5)


scala> val Vector2 = Vector(5,6,7,8,9,10,10)
Vector2: scala.collection.immutable.Vector[Int] = Vector(5,6, 7, 8, 9, 10, 10)


Most of the methods in Vector have the same behavior that we have seen in the case of Arrays here. We will see some of  methods that can be applied on Vectors:

1) :+ : An element is appended to vector

scala> Vector1 :+ 100
res3: scala.collection.immutable.Vector[Int] = Vector(1, 1, 2, 3, 4, 5, 100)


2)  +: : An element is prepended to array

scala> 100 +: Vector1
res4: scala.collection.immutable.Vector[Int] = Vector(100, 1, 1, 2, 3, 4, 5)


3)  addString : Takes a StringBuilder and returns a StringBuilder with all elements appended with a start, end and a separator

scala> Vector1 addString (b=new StringBuilder("Hello"),start=" Start ",sep=":",end=" End ")
res9: StringBuilder = Hello Start 1:1:2:3:4:5 End


4) aggregate : Aggregates results after application of  an operator to different elements

scala> Vector(1,1,2,3,4,5).aggregate(0)({ (sum, i) => sum + i }, { (p1, p2) => p1 + p2 })
res13: Int = 16


5) apply : Selects element by index

scala> Vector1 apply 1
res14: Int = 1


6)  diff : Returns difference between operands

scala> Vector1 diff Vector2
res24: scala.collection.immutable.Vector[Int] = Vector(1, 1, 2, 3, 4)


7) iterator :  Returns a Vector Iterator. This iterator can then be used to iterate over the different elements of the Vector

scala> val Vector1_Iterator = Vector1.iterator
Vector1_Iterator: scala.collection.immutable.VectorIterator[Int] = non-empty iterator

scala> while (Vector1_Iterator.hasNext) {
     |   println(Vector1_Iterator.next())
     | }
1
1
2
3
4
5


8) reverseIterator : Returns a Iterator with elements in reverse order

scala> while (Vector1_Reverse_Iterator.hasNext) {
     |   println(Vector1_Reverse_Iterator.next())
     | }
5
4
3
2
1
1


9) union : Returns union of operands

scala> Vector1 union Vector2
res29: scala.collection.immutable.Vector[Int] = Vector(1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 10)


10) zipWithIndex: Zips the vector with its indices

scala> Vector1.zipWithIndex
res30: scala.collection.immutable.Vector[(Int, Int)] = Vector((1,0), (1,1), (2,2), (3,3), (4,4), (5,5))


This concludes the discussion of Vectors in Scala.