Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Created February 14, 2013 08:01
Show Gist options
  • Save tpolecat/4951242 to your computer and use it in GitHub Desktop.
Save tpolecat/4951242 to your computer and use it in GitHub Desktop.
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