Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Last active December 30, 2017 16:04
Show Gist options
  • Save yasuabe/c9ec331cc64910e82839de5117060bc5 to your computer and use it in GitHub Desktop.
Save yasuabe/c9ec331cc64910e82839de5117060bc5 to your computer and use it in GitHub Desktop.
arrow tutorial '01 the arrow'
import cats.arrow.Arrow
import cats.data.{Cokleisli, NonEmptyList}
import cats.instances.function._
import cats.syntax.compose._
val f1 = Arrow[Function].lift(1d / (_: Int))
f1(10)
val A = Arrow[Function]
// >>> は compose 構文
val f2: Function[Int, String] = A.lift(1d / (_: Int)) >>> A.lift(s => s"$s%")
f2(10)
// first
val f3: Function[(Int, Symbol), (Double, Symbol)] = A.first(1d / _)
f3((10, 'ten))
// second
val f4: Function[(Int, Symbol), (Int, String)] = A.second(_.toString.reverse)
f4((10, 'ten))
// first >>> second
val f5: Function[(Int, Symbol), (Double, String)] =
A.first(1d / (_: Int)) >>> A.second(_.toString.reverse)
f5((10, 'ten))
// cokleisli
val cokleisliArrow = Arrow[Cokleisli[NonEmptyList, ?, ?]]
val f6: Cokleisli[NonEmptyList, String, Int] = cokleisliArrow.lift(_.length)
f6.run(NonEmptyList("abc", List("ab", "c")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment