Last active
August 29, 2015 14:17
-
-
Save iximeow/a924f073daa14ffd48cb to your computer and use it in GitHub Desktop.
lazy
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
| trait Lazy[T] { | |
| def value: T | |
| def map[U](f: T => U): Lazy[U] | |
| def flatten[U](implicit lazyConv: (=> T) => Lazy[U]): Lazy[U] | |
| def flatMap[U](f: T => Lazy[U]): Lazy[U] | |
| } | |
| object Lazy { | |
| implicit def lazyIdentity[A]: (=> Lazy[A]) => Lazy[A] = (old) => Lazy { old.value } | |
| def apply[T](t: => T): Lazy[T] = new Lazy[T] { | |
| lazy val value = t | |
| def map[U](f: T => U): Lazy[U] = Lazy(f(t)) | |
| def flatten[U](implicit lazyConv: (=> T) => Lazy[U]): Lazy[U] = | |
| lazyConv(t) | |
| def flatMap[U](f: T => Lazy[U]): Lazy[U] = map(f).flatten | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment