Skip to content

Instantly share code, notes, and snippets.

@jaimefjorge
Created November 2, 2011 13:40
Show Gist options
  • Save jaimefjorge/1333642 to your computer and use it in GitHub Desktop.
Save jaimefjorge/1333642 to your computer and use it in GitHub Desktop.
List to TupleN
object ListToTupleImplicit {
class ListToTuple[A](xs:List[A]){
def toTuple2 = (xs(0),xs(1))
def toTuple3 = (xs(0),xs(1),xs(2))
def toTuple4 = (xs(0),xs(1),xs(2),xs(3))
}
implicit def listToTuple[A](xs:List[A]):ListToTuple[A] = new ListToTuple(xs)
}
scala> import ListToTupleImplicit._
import ListToTupleImplicit._
scala> List(1,2,3,4).toTuple4
res14: (Int, Int, Int, Int) = (1,2,3,4)
scala> List(1,2,3,4).toTuple3
res15: (Int, Int, Int) = (1,2,3)
scala> def baz(a:Int,b:Int,c:Int) = a + b + c
baz: (a: Int, b: Int, c: Int)Int
scala> (baz _).tupled(List(1,2,3).toTuple3)
res16: Int = 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment