Last active
August 29, 2015 14:08
-
-
Save noelmarkham/f529bc4bae993854e800 to your computer and use it in GitHub Desktop.
Composing Functors
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
scala> val FL = Functor[List] | |
FL: scalaz.Functor[List] = scalaz.std.ListInstances$$anon$1@575f20e3 | |
scala> val FF = Functor[Future] | |
FF: scalaz.Functor[scala.concurrent.Future] = scalaz.std.FutureInstance@678bc1a9 | |
scala> val FF_FL = FF compose FL | |
FF_FL: scalaz.Functor[[α]scala.concurrent.Future[List[α]]] = scalaz.Functor$$anon$1@49487384 | |
scala> val values = Future(List(1, 2, 3, 4, 5)) | |
values: scala.concurrent.Future[List[Int]] = scala.concurrent.impl.Promise$DefaultPromise@16d8bdcc | |
scala> val mapped = FF_FL.map(values)(_ + 100) | |
mapped: scala.concurrent.Future[List[Int]] = scala.concurrent.impl.Promise$DefaultPromise@8ecd29 | |
scala> mapped.foreach(println) | |
List(101, 102, 103, 104, 105) | |
//////////// | |
def functorCompose[F[_], G[_], A, B](value: F[G[A]])(mapping: A => B)(implicit functorF: Functor[F], functorG: Functor[G]): F[G[B]] = { | |
(functorF compose functorG).map(value)(mapping) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment