Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tupple ware in scala

414 views

Published on

These slides introduce the enthusiasts to Tupples and their usage in Scala including some interesting hacks and possibilities.

  • Be the first to comment

  • Be the first to like this

Tupple ware in scala

  1. 1. Tupple-ware in Scala By VulcanMinds
  2. 2. Tupple - ConceptsScala provides an interesting collection-like structure with 2 or more ‘Any’ typeelements called Tupples which extends a Product.for e.g. ("Jane", "jane@jane.edu",24," Oregon, USA")is a tupple.scala> val y = ("Jane", "jane@jane.edu",24," Oregon, USA")y: (java.lang.String, java.lang.String, Int, java.lang.String) = (Jane,jane@jane.edu,24," Oregon, USA")The number of elements can be found by <tupple>.productArityfor e.g. number of elements in Jane tupple is.scala> y.productArityres7: Int = 4scala> val u = (100,200)u: (Int, Int) = (100,200)scala> u.productArityres6: Int = 2
  3. 3. Simple Scala hacks for TupplesAccessing individual members......scala> u._1res142: Int = 100scala> u._2res143: Int = 200Scala Hacks ......scala> 1->2res79: (Int, Int) = (1,2)ORscala> val c = 1->2c: (Int, Int) = (1,2)ORscala> val u = "Manish" -> 29u: (java.lang.String, Int) = (Manish,29)
  4. 4. Scala hacks for Tupples - continuedscala> 1->2->3res80: ((Int, Int), Int) = ((1,2),3)ORscala> 1->2->3->4res81: (((Int, Int), Int), Int) = (((1,2),3),4)ORscala> ("manish"->2) -> ("sandesh", 32)res106: ((java.lang.String, Int), (java.lang.String, Int)) = ((manish,2),(sandesh,32))ORscala> val y = ("manish"->2) -> ("sandesh", 32) -> ("kat", 44)y: (((java.lang.String, Int), (java.lang.String, Int)), (java.lang.String, Int)) = (((manish,2),(sandesh,32)),(kat,44))
  5. 5. Applying Functions to TupplesA simple scala function with non-tupple parameter A simple scala function with a tupplescala> def product(x:Int,y:Int) = { parameter | x*y }product: (x: Int, y: Int)Int scala> def product2(x:(Int,Int)) = { | x._1 * x._2}scala> val ff = product _ product2: (x: (Int, Int))Intff: (Int, Int) => Int = <function2> scala> val s = (100,200)scala> val h = ff(100,200) s: (Int, Int) = (100,200)h: Int = 20000----------------------------------------------------------------------- scala> val g = product2 _Converting the function to a ‘tuppled’ type g: ((Int, Int)) => Int = <function1>scala> val fft = ff.tupled scala> g((100,200))fft: ((Int, Int)) => Int = <function1> res147: Int = 20000scala> fft((100,200))res146: Int = 20000*Note the function parameter passed is of type tupple (Int,Int)
  6. 6. Returning Tupples with variable number of elementsscala> def compute(x:Int) = { | x match { | case 1 => (1,2) | case 2 => (1,2,3) | case _ => ("Unknown Number?",0) |} |}compute: (x: Int)Product with Serializablescala> val q = compute _q: Int => Product with Serializable = <function1>scala> val e = q(1)e: Product with Serializable = (1,2)scala> val e = q(16)e: Product with Serializable = (Unknown Number?,0)
  7. 7. Playing with Null types in TupplesPlaying with Nulls in tupplesscala> val x = (200, null)x: (Int, Null) = (200,null)scala> x match { | case (i:Int, v) => println("got tuple (%s: %s)".format(i, v)) | case _ => println("catch all") |}got tuple (200: null)
  8. 8. Processing values in tupplesprocessing values in tupples// a list of people with their names and gender and agesscala> val w = List(("manish","male", 22),("kat","female",32),("Jose","male",30),("Obama","male",35),("Ken","male",44),("Hanna","female",21))w: List[(java.lang.String, java.lang.String, Int)] = List((manish,male,22), (kat,female,32), (Jose,male,30), (Obama,male,35),(Ken,male,44), (Hanna,female,21))scala> def countBoth(x:List[(String,String,Int)]) = { | var males = 0 | var females = 0 | for(person <- x) { | if(person._2 equals "male") males = males + 1 | else females = females + 1 |} | (males, females) |}countBoth: (x: List[(String, String, Int)])(Int, Int)scala> val q = countBoth _q: List[(String, String, Int)] => (Int, Int) = <function1>scala> val result = q(w)result: (Int, Int) = (4,2)Isn’t that concise?

×