Skip to content

Instantly share code, notes, and snippets.

@milessabin
Last active December 20, 2015 05:49
Show Gist options
  • Save milessabin/6081113 to your computer and use it in GitHub Desktop.
Save milessabin/6081113 to your computer and use it in GitHub Desktop.
Slicing and dicing tuples in shapeless 2.0.0-SNAPSHOT.
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment