Created
June 27, 2012 05:01
-
-
Save imeredith/3001602 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
scala> trait Functor[F[_]] { def a[A, B](b: A => B): F[A] => F[B]} | |
defined trait Functor | |
scala> object Test extends Functor[Option] { def a[A, B](b: A=>B) = (a: Option[A]) => a map b} | |
defined module Test | |
scala> Test.a((a: String) => a.length) | |
res8: (Option[String]) => Option[Int] = <function1> | |
scala> res8(Some("teSt")) | |
res10: Option[Int] = Some(4) | |
scala> res8(None) | |
res12: Option[Int] = None | |
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
new Monad[IntF] { | |
def map[A, B](f: A => B) = (a: IntF[A]) => IntF((i: Int) => f(a.k(i))) | |
def flatMap[A, B](f: A => IntF[B]) = (a: IntF[A]) => IntF((i: Int) => f(a.k(i)).k(i)) | |
def point[A](a: A) = IntF((i: Int) => a) | |
} | |
scala> res3.flatMap((s: String) => res3.point(s.length())) | |
res16: (IntF[String]) => IntF[Int] = <function1> | |
scala> res16(IntF((a:Int) => a.toString)) | |
res17: IntF[Int] = IntF(<function1>) | |
scala> res17.k(4) | |
res18: Int = 1 | |
scala> res17.k(40) | |
res19: Int = 2 | |
scala> res17.k(400) | |
res20: Int = 3 | |
scala> res17.k(4000) | |
res21: Int = 4 |
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
scala> new Functor[IntF] { def a[A,B](b: A=>B) = (a: IntF[A]) => IntF((c:Int) => b(a.k(c)))} | |
res16: java.lang.Object with Functor[IntF] = $anon$1@6ba26eb1 | |
scala> res16.a((a: String) => a.length) | |
res17: (IntF[String]) => IntF[Int] = <function1> | |
scala> res18.k(3) | |
res22: Int = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment