public
Last active

Slicing and dicing tuples in shapeless 2.0.0-SNAPSHOT.

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
 
scala> import shapeless._
import shapeless._
 
scala> import syntax.tuple._
import syntax.tuple._
 
scala> val t = (23, "foo", true, 2.0, ())
t: (Int, String, Boolean, Double, Unit) = (23,foo,true,2.0,())
 
scala> t.head
res0: Int = 23
 
scala> t.tail
res1: (String, Boolean, Double, Unit) = (foo,true,2.0,())
 
scala> t.take(2)
res2: (Int, String) = (23,foo)
 
scala> t.drop(3)
res3: (Double, Unit) = (2.0,())
 
scala> t.split(2)
res4: ((Int, String), (Boolean, Double, Unit)) = ((23,foo),(true,2.0,()))
 
scala> t(3)
res5: Double = 2.0
 
scala> t(5) // Doesn't compile
<console>:15: error: could not find implicit value for parameter at: shapeless.ops.tuple.TupleAt[(Int, String, Boolean, Double, Unit),nat_1.N]
t(5) // Doesn't compile
^
 
scala> (23, "foo") :+ true
res7: (Int, String, Boolean) = (23,foo,true)
 
scala> 23 +: ("foo", true)
res8: (Int, String, Boolean) = (23,foo,true)
 
scala> (23, "foo") ++ (true, 2.0)
res10: (Int, String, Boolean, Double) = (23,foo,true,2.0)
 
scala> (23, "foo", true) map option
res11: (Option[Int], Option[String], Option[Boolean]) = (Some(23),Some(foo),Some(true))
 
scala> ((23, "foo"), (), (true, 2.0, "bar")) flatMap identity
res12: (Int, String, Boolean, Double, String) = (23,foo,true,2.0,bar)
 
scala> (1, 2, 3) zip ("one", "two", "three")
res13: ((Int, String), (Int, String), (Int, String)) = ((1,one),(2,two),(3,three))
 
scala> ((1, "foo"), (2, "bar"), (3, "baz")).unzip
res14: ((Int, Int, Int), (String, String, String)) = ((1,2,3),(foo,bar,baz))
 
scala> ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0)).transpose
res15: ((Double, Double, Double), (Double, Double, Double), (Double, Double, Double)) = ((1.0,4.0,7.0),(2.0,5.0,8.0),(3.0,6.0,9.0))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.