Skip to content

Instantly share code, notes, and snippets.

@iximeow
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save iximeow/a924f073daa14ffd48cb to your computer and use it in GitHub Desktop.

Select an option

Save iximeow/a924f073daa14ffd48cb to your computer and use it in GitHub Desktop.
lazy
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