![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
trait Applicative[T[_]] extends Functor[T] { def pure[A](a:A):T[A] def ap[A,B](tf:T[A=>B]): T[A] => T[B] def map[A,B](f:A=>B) = ap(pure(f)) } abstract class Applicable[A, B, T[_]](fs: T[A=>B]) { def <*>(at: T[A]):T[B] } abstract class Lifted[A, B, T[_]](f: A=>B) { def <@>(ta:T[A]): T[B] // in Haskell it is <$>; can't use $ in Scala, and can't have lt;&s> either. } trait RichApplicative[T[_]] extends Applicative[T] { implicit def applicable[A,B](tf:T[A=>B]): Applicable[A, B, T] implicit def lift[A,B](f: A=>B) = new Lifted[A, B, T](f) { def <@>(at: T[A]) = ap(pure(f))(at)} } trait Traversable[T[_]] extends RichApplicative[T] { def traverse[A, B](f: A => T[B])(as: List[A]): T[List[B]] = as match { case Nil => pure(Nil) case head::tail => lift(cons[B] _) <@> f(head) <*> traverse(f)(tail) } def dist[A] = traverse(identity[T[A]] _) _ }
Work in progress. I'm on page 5. (dumb, eh)
Comments? Ideas?
no subject
Date: 2012-04-11 02:54 pm (UTC)no subject
Date: 2012-04-11 04:41 pm (UTC)no subject
Date: 2012-04-11 04:44 pm (UTC)no subject
Date: 2012-04-11 04:53 pm (UTC)no subject
Date: 2012-04-11 06:01 pm (UTC)