Created
February 14, 2013 08:01
-
-
Save tpolecat/4951242 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Nafg { | |
object Tuples { | |
trait HT[A] { | |
type H | |
type T | |
def hd(a: A): H | |
def tl(a: A): T | |
} | |
implicit def t2[A, B] = new HT[(A, B)] { | |
type H = A | |
type T = B | |
def hd(t: (A, B)) = t._1 | |
def tl(t: (A, B)) = t._2 | |
} | |
implicit def t3[A, B, C] = new HT[(A, B, C)] { | |
type H = A | |
type T = (B, C) | |
def hd(t: (A, B, C)) = t._1 | |
def tl(t: (A, B, C)) = (t._2, t._3) | |
} | |
implicit def t4[A, B, C, D] = new HT[(A, B, C, D)] { | |
type H = A | |
type T = (B, C, D) | |
def hd(t: (A, B, C, D)) = t._1 | |
def tl(t: (A, B, C, D)) = (t._2, t._3, t._4) | |
} | |
def hd[A](a: A)(implicit ht: HT[A]) = ht.hd(a) | |
def tl[A](a: A)(implicit ht: HT[A]) = ht.tl(a) | |
implicit class Id[A](a:A) { | |
def head(implicit ht: HT[A]) = hd(a) | |
def tail(implicit ht: HT[A]) = tl(a) | |
} | |
val tup = (1, "2", false) | |
val a: Int = tup.head | |
val bc: (String, Boolean) = tup.tail | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment