Created
January 8, 2015 11:18
-
-
Save ryoppy/aff26202865bc17f608a to your computer and use it in GitHub Desktop.
めも
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
import scalaz._, Scalaz._ | |
val f = (_: Int) + 1 | |
val g = (_: Int) + 2 | |
val h = for { | |
x <- f | |
y <- g | |
} yield { | |
x + y | |
} | |
// (_ + 1) + (_ + 2) | |
// (1 + 1) + (1 + 2) | |
// 5 | |
assert(h(1) === 5) | |
// 同上 | |
val q = (f |@| g)(_ + _) | |
assert(q(1) === 5) | |
// f(1) : 2 | |
val w = f <* g | |
assert(w(1) === 2) | |
// クライスリ | |
val kf = Kleisli[Option, Int, Int] { x: Int => Some(x + 1) } | |
val kg = Kleisli[Option, Int, Int] { x: Int => Some(x * 2) } | |
locally { | |
// andThen | |
val f = kf >=> kg | |
assert(f(1) === 4.some) | |
// compose | |
val g = kf <=< kf | |
assert(g(1) === 3.some) | |
// andThen (kelisliでなく関数を受け取れる) | |
val h = kf >==> ((x:Int) => x.some) | |
assert(h(1) === 2.some) | |
} | |
locally { | |
val h = for { | |
f <- kf | |
g <- kg | |
} yield f + g // (1 + 1) + (1 * 2) = 4 | |
assert(h(1) === 4.some) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment