Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created November 11, 2012 05:35
Show Gist options
  • Save xuwei-k/4053842 to your computer and use it in GitHub Desktop.
Save xuwei-k/4053842 to your computer and use it in GitHub Desktop.
Semigroupoid in scala
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
trait FlatMap[F[_]] extends Functor[F] {
def flatMap[A, B](f: A => F[B]): F[A] => F[B]
}
trait Semigroupoid[~>[_, _]] {
def compose[A, B, C]: (A ~> B) => (B ~> C) => (A ~> C)
}
case class Kleisli[A, F[_], B](k: A => F[B])
def KleisliSemigroupoid[F[_]: FlatMap]: Semigroupoid[({ type lam[a, b]=Kleisli[a, F, b] })#lam]
= new Semigroupoid[({ type lam[a, b]=Kleisli[a, F, b] })#lam] {
def compose[A, B, C] = f => g => Kleisli(a => implicitly[FlatMap[F]].flatMap(g.k)(f k a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment