Skip to content

Instantly share code, notes, and snippets.

@ahoy-jon
Last active December 15, 2015 07:38
Show Gist options
  • Save ahoy-jon/5224356 to your computer and use it in GitHub Desktop.
Save ahoy-jon/5224356 to your computer and use it in GitHub Desktop.
object SomeHelper {
implicit def lazyFunc[A,B](f: A=> B): ((=> A) => B) = a => f(a)
}
object MyApp extends App {
import SomeHelper._
val m = new SomeFunctor(1)
val g: (Int) => String = x => sys.error("g raise an error")
val f: (=> String) => Boolean = x => try { x; true } catch { case e: Throwable => false }
m.map(x => f(g(x))).foreach(println)
m.map(g).map(x => f(x)).foreach(println)
}
class SomeFunctor[A](a: => A) {
def map[B](f: (=> A) => B):SomeFunctor[B] = new SomeFunctor(f(a))
def foreach(f: A => Unit):Unit = f(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment