Last active
October 31, 2016 15:01
-
-
Save fomkin/71d763b2ff8d7eaf16850e732f686a2d to your computer and use it in GitHub Desktop.
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
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