Skip to content

Instantly share code, notes, and snippets.

@yanns
Last active June 11, 2018 14:55
Show Gist options
  • Save yanns/d2a275462f8381e4eb953a2e13b16496 to your computer and use it in GitHub Desktop.
Save yanns/d2a275462f8381e4eb953a2e13b16496 to your computer and use it in GitHub Desktop.
type Algebra[F[_], A] = F[A] => A
def cata[F[_], R, A](algebra: Algebra[F, A], out: R ⇒ F[R])(r: R)(implicit F: Functor[F]): A =
algebra(F.map(cata(algebra, out))(out(r)))
type Coalgebra[F[_], A] = A => F[A]
def ana[F[_], R, A](coalgebra: Coalgebra[F, A], in: F[R] ⇒ R)(a: A)(implicit F: Functor[F]): R =
in(F.map(ana(coalgebra, in))(coalgebra(a)))
def hylo[F[_], A, B](coalgebra: Coalgebra[F, A], algebra: Algebra[F, B])
(a: A)(implicit F: Functor[F]): B =
algebra(F.map(hylo(coalgebra, algebra))(coalgebra(a)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment