Monday, September 12, 2016

Tuple Example : Tuple2, Tuple3, _ etc..

scala> /*
     |    Creating Tuple #86
     |    Note : A Tuple can contain elements of different type
     |  */
     | val tuple2:Tuple2[String, Int] = new Tuple2("maths", 75)
tuple2: (String, Int) = (maths,75)

scala> val tuple2 = ("maths", 75)
tuple2: (String, Int) = (maths,75)

scala>

scala> val tuple3:Tuple3[String, Int, String] = new Tuple3("english", 75, "pass")
tuple3: (String, Int, String) = (english,75,pass)

scala> val tuple3 = ("english", 75, "pass")
tuple3: (String, Int, String) = (english,75,pass)

scala>

scala> /*
     |    Accessing Tuple
     |    Note here _1 & _3 are fields. Unlike List we do not have apply()
     |    method to access the fields...the reason being each element of
     |    the tuple might be different type..whereas apply() method always
     |    returns an element of the same type
     | */
     | tuple2._1
res61: String = maths

scala> tuple3._3
res62: String = pass