Friday 29 December 2017

Introduction to Scala - XIV

We continue the journey on Scala on the topic of Sets 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 Sets are defined along with some features of Sets in Scala.

Sets are the same in Scala as in other languages: It does not contain duplicates.

We define a couple of Sets below and, then, use them in different operations. We can create Sets as follows:

1) An empty Set:

val Set0: Set[Int] = Set()
Set0

The results are shown below:

scala> val Set0: Set[Int] = Set()
Set0: Set[Int] = Set()

scala> Set0
res9: Set[Int] = Set()


2) A Set initialized with values:

var Set1 = Set(1, 1, 2, 3, 4, 5)
var Set2 = Set(5, 6, 7, 8, 9, 10, 10)


The results are shown below:

scala> var Set1 = Set(1, 1, 2, 3, 4, 5)
Set1: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> var Set2 = Set(5, 6, 7, 8, 9, 10, 10)
Set2: scala.collection.immutable.Set[Int] = Set(5, 10, 6, 9, 7, 8)


For the sake of technicality, the above sets are immutable. They cannot be modified in place. On the other hand, a mutable Set can be modified. Mutable Sets are created as follows:

scala> var MutableSet1 = collection.mutable.Set(1,2,3,4,5)
MutableSet1: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 4)


Different operations can be performed on Sets. Many of them are similar to those described on Arrays and Vectors. We will describe some of the operations below:

1)  & : Returns elements that are common

scala> Set1 & Set2
res14: scala.collection.immutable.Set[Int] = Set(5)


2)  &~ : Returns elements in first set not present in second set

scala> Set1 &~ Set2
res15: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)


3)  + : Can add one or multiple arguments based on parameters to a Set but ensures that the final Set has no duplicates

scala> Set1
res19: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> Set1 + 0
res20: scala.collection.immutable.Set[Int] = Set(0, 5, 1, 2, 3, 4)


scala> Set1 + (6,7,8)
res21: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 7, 3, 8, 4)


scala> Set1
res22: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)


4) - : Can remove an element or more elements depending on parameters

scala> Set1
res22: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> Set1 - 5
res23: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)

scala> Set1 - (1,2,3,3,3)
res24: scala.collection.immutable.Set[Int] = Set(5, 4)

scala> Set1
res25: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)


5) -- : Returns elements from first Set after removing elements from second Set

scala> Set1 -- Set2
res38: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)


6) fold : Returns a result after folding the elements defined on an operation using a specified associative binary operator starting with an initial value as shown below:

scala> Set1
res49: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> Set1.fold (100) (_ + _)
res50: Int = 115

scala> (Set1 fold 100) (_ + _)
res51: Int = 115


7) groupBy: Partitions the set into maps based on a discriminator function

scala> Set1
res60: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> Set1 groupBy [Int] (_%2)
res61: scala.collection.immutable.Map[Int,scala.collection.immutable.Set[Int]] = Map(1 -> Set(5, 1, 3), 0 -> Set(2, 4))

8)  subsetOf : Returns boolean if set is a subset

scala> Set1
res63: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> Set(1,2) subsetOf Set1
res64: Boolean = true


9) toString : Returns a string

scala> Set1.toString
res66: String = Set(5, 1, 2, 3, 4)


10) union or | : Returns union of two sets

scala> Set1 union Set2
res75: scala.collection.immutable.Set[Int] = Set(5, 10, 1, 6, 9, 2, 7, 3, 8, 4)

scala> Set1 | Set2
res76: scala.collection.immutable.Set[Int] = Set(5, 10, 1, 6, 9, 2, 7, 3, 8, 4)


11) zip : Returns combined pairs as shown below:

scala> Set1
res68: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> Set2
res69: scala.collection.immutable.Set[Int] = Set(5, 10, 6, 9, 7, 8)

scala> Set1 zip Set2
res70: scala.collection.immutable.Set[(Int, Int)] = Set((3,9), (4,7), (1,10), (2,6), (5,5))


This concludes the discussion of Sets in Scala