Skip to content

Instantly share code, notes, and snippets.

@ryoppy
Created January 8, 2015 11:18
Show Gist options
  • Save ryoppy/aff26202865bc17f608a to your computer and use it in GitHub Desktop.
Save ryoppy/aff26202865bc17f608a to your computer and use it in GitHub Desktop.
めも
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