Created
November 2, 2011 13:40
-
-
Save jaimefjorge/1333642 to your computer and use it in GitHub Desktop.
List to TupleN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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