6 June 2013

Scala notes 6: Tuple definition and example

Information: THIS IS NOT TUTORIAL.
Scala notes is series of my "side comments" that can be useful during learning Scala.
Their contains bunch of information about interesting bits, explanation of things that i didn't come across yet or attempt to explain things which i didn't understand.
Tuple.

Finding definition was not a easy task.
Official Docs has no Tuple class,
but it has:
Tuple1,Tuple2 in scala package
Tuple2Zipped, Tuple3Zipped in scala.runtime package

Only iformation ,which you can find is:
" A tuple of 1 elements; the canonical representation of a scala.Product1." (source: http://www.scala-lang.org/api/current/index.html#scala.Tuple1)


I have no idea what does means.
It has no meaning for me.

So what it is Tuple?Tuple is a list ("collection") of ordered and/or unordered elements of multiple types.

What is a difference between tuples and list?
List contains collection of one type,where tuples can contains collection of different types.

You can create Tuple with 1 to 22 elements .
Why 22?Why not. For whatever reasons.
(there is no official explanation for that.There are many "theories" about from "reasonable sounds like" jvm limitation up to "bizzare explanations" like ... "creator's dream size of penis" . :/ )

How to access to tuple elements?
You can access tuple elements using ._N where N is a number
for example:

package pastor.ovh.org

object TuplesLab {
  def main(args: Array[String]) = {

    def lol(a: Array[Int]): (Int,Int) = {
      return (a(0),a.length)
    }

   val tupleX = lol(Array(1, 2, 3, 4, 5, 6, 7, 8))

   println("First element in array is " + tupleX._1 + " and last " + tupleX._2)
}
}
Is Tuple is a Collection?
Answer is No  ...BUT ...  it doesn't have stuff which collection should have in Scala,but .. you can iterate over elements of Tuples.

What can be use useful for?
I found useful as return two and more values from a method,so i don't need create "retun class" to return collection of different type of data.

Update:
I found another interesting thing on Nishan Patel's blog (http://nishanpatel.wordpress.com/2013/01/17/tuple-in-scala/)

"Now if you aware of Scala or java collection you might having question why we have to use dot, underscore and tuple element index to access the values from tuple. Now in Scala when you write tuple(1) Scala complier call apply method and if you see Scala API apply method always return same type of elements and as I said earlier, Scala tuple is collection of different type of elements." Nishan Patel 
Example:
/**
* Author: Pastor cmentarny
* A Tuple example
*/

object TuplesLab {

   def main(args: Array[String]) = {

     def mixavemax(a: Array[Int]): (Int, String, Int) = {
     var average: String = "Average of " + a.length + " elements is: "

     if (a.length == 0) {
       return (0, "?", 0)
     } else if (a.length == 1) {
      return (a(0), average + av(a), a(0))
     } else {
      val tuples = mm(a) // example of assign tuple as value
 
     tuples.productIterator.foreach(println) //example of iterate though elements of Tuple
     return (tuples._1, average + av(a), tuples._2) //example of use 2 elements of tuple defined above
    }
   }

   def av(c: Array[Int]): Int = {
    var total: Int = 0

    for (i <- 0 to (c.length-1)) {
     total += c(i)
    }
   return total
   }

   def mm(d: Array[Int]): (Int, Int) = { //define tuple as return
    var mi = d(0)
    var ma = d(0)
    for (j <- 0 to (d.length-1)) {
     if (d(j) < mi) {
      mi = d(j)
     }
     if (d(j) > ma) {
      ma = d(j)
     }
    }
    return (mi, ma)
   }

   println (mixavemax(Array(1,2,3,4,5,6,7,8))) // run example
   }
}
Summary:
Tuple is a collection of elements of multiple types.
There is no official explanation why is end on 22.
Tuples are immutable
Tuples are first-class values (it means you can assign them to variables, pass them as values ,return them from methods)
There are few ways to define a tuple.


Resources:
http://www.javacodegeeks.com/2011/09/scala-tutorial-tuples-lists-methods-on.html
http://nishanpatel.wordpress.com/
http://stackoverflow.com/
http://www.scala-lang.org/

No comments:

Post a Comment