Skip to content

Instantly share code, notes, and snippets.

@globulon
Created February 5, 2012 09:40
Show Gist options
  • Select an option

  • Save globulon/1744442 to your computer and use it in GitHub Desktop.

Select an option

Save globulon/1744442 to your computer and use it in GitHub Desktop.
First shot for applicative functor example
package com.promindis.patterns
trait ApplicativeHelper[F[_]] extends MonadHelper[F] {
def apply[U](data: U): F[U]
def apply[U](input: F[U]): Applicative[U, F]
}
trait Applicative[+T, F[_]] extends Monad[T, F] {
def :*:[P >: T, U](fs: F[P => U])(implicit h: ApplicativeHelper[F]): F[U] = {
flatMap(x => h(fs).map(f => f(x)))
}
def :@:[R, Q >: T](f: Q => R) = map(f)
}
object Applicative {
def liftA2[T, P, Q, A[_]](f: (T, P) => Q, a1: A[T], a2: A[P])(implicit h: ApplicativeHelper[A]): A[Q] = {
(f.curried :@: h(a1)) :*: h(a2)
}
def sequence[T, A[_]](input: List[A[T]])(implicit h: ApplicativeHelper[A]): A[List[T]] = {
input match {
case (x :: xs) =>
def cons(head: T, list: List[T]): List[T] = head :: list
liftA2(cons, x, sequence(xs))
case _ => h(List.empty[T])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment