Created
December 30, 2017 16:07
-
-
Save yasuabe/b41a6475793900a1d8fce4af78200b4a to your computer and use it in GitHub Desktop.
arrow tutorial '5. kleisli arrows'
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 cats.arrow.Arrow | |
import cats.data.Kleisli | |
sealed case class SimpleFunc[A, B](runF: A => B) | |
import cats.syntax.arrow._ | |
import cats.syntax.compose._ | |
def arr[F[_, _]: Arrow, A, B](f: A => B): F[A, B] = Arrow[F] lift f | |
def split[F[_, _]: Arrow, A]: F[A, (A, A)] = arr(x => (x, x)) | |
def unsplit[F[_, _]: Arrow, A, B, C](f: (A, B) => C): F[(A, B), C] = | |
arr(f.tupled) | |
def liftA2[F[_, _], A, B, C, D](f: (B, C) => D, fb: F[A, B], fc: F[A, C]) | |
(implicit A: Arrow[F]): F[A, D] = | |
(fb &&& fc) >>> unsplit(f) | |
// split[F, A] >>> A.first(fb) >>> A.second(fc) >>> unsplit(f) | |
// Kleisli ------------------ | |
type ListKleisli[A, B] = Kleisli[List, A, B] | |
def testKleisliArrow(n: Int)(implicit A0: Arrow[ListKleisli]) = { | |
type Arr = ListKleisli[Int, Int] | |
def plusMinus:Arr = Kleisli(x => x :: (-x) :: Nil) | |
def double :Arr = arr((_: Int) * 2) | |
def h2 :Arr = liftA2((_: Int) + (_: Int), plusMinus, double) | |
h2 run n | |
} | |
import cats.instances.list._ | |
testKleisliArrow(8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment