Skip to content

Instantly share code, notes, and snippets.

@fomkin
Last active October 31, 2016 15:01
Show Gist options
  • Save fomkin/71d763b2ff8d7eaf16850e732f686a2d to your computer and use it in GitHub Desktop.
Save fomkin/71d763b2ff8d7eaf16850e732f686a2d to your computer and use it in GitHub Desktop.
sealed trait MyList[+T]
case object MyNil extends MyList[Nothing]
case class MyCons[+T](head: T, tail: MyList[T]) extends MyList[T]
object MyList {
object :: {
def unapply[T](list: MyList[T]): Option[(T, MyList[T])] = list match {
case MyCons(x, xs) => Some((x, xs))
case MyNil => None
}
}
def apply[T](x: T, xs: MyList[T]): MyList[T] = MyCons(x, xs)
}
import MyList._
val list = MyList(1, MyList(2, MyList(3, MyNil)))
val a :: b :: c :: xs = list
// Output:
// a: Int = 1
// b: Int = 2
// c: Int = 3
// xs: MyList[Int] = MyNil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment