Skip to content

Instantly share code, notes, and snippets.

@ryoppy
Created January 13, 2015 09:21
Show Gist options
  • Save ryoppy/d3cd92cab4c752da3418 to your computer and use it in GitHub Desktop.
Save ryoppy/d3cd92cab4c752da3418 to your computer and use it in GitHub Desktop.
めも
import scalaz._, Scalaz._
val f = (_: Int) + 1
val g = (_: Int) * 2
// Arrow
// ========
locally {
// andThen
assert((f >>> g)(1) === 4)
// compose
assert((f <<< g)(1) === 3)
// 関数をあわせる
assert((f *** g)(1, 1) === (2, 2))
// 1つの引数でどっちの関数にも適用
assert((f &&& g)(1) === (2, 2))
}
// Unapply
// 色々な型の組み替え方法
// TraverseOps[F[_],A]
// ========
locally {
// traverse(f: A => G[B]): G[F[B]]
assert(1.some.traverse[List, Int]((x: Int) => List(x)) === List(Option(1)))
// sequence[G[_], B]: G[F[B]]
assert(Option(List(1)).sequence[List, Int] === List(Option(1)))
// 型を2つ以上とる場合はUnapplyを利用したtraverseUが使える
val a = 1.some.traverse[({type λ[A]=Validation[String, A]})#λ, Int](_.success[String])
val b = 1.some.traverseU(_.success[String])
assert(a == b)
// sequenceUも
val c = Option(1.success[String]).sequence[({type λ[A]=Validation[String, A]})#λ, Int]
val d = Option(1.success[String]).sequenceU
assert(c == d)
// traverseM[G[_], B](f: A => G[F[B]]): G[F[B]]
val e = Option(List(1)).traverseM[List, String](_.map(_ + "!" some))
assert(e === List(Option("1!")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment