Permalink
Browse files

broken for now. Please don't look here.

  • Loading branch information...
retronym committed Apr 23, 2010
1 parent 6fe6729 commit bd3807e114407d0fb136de8bb521d94078160bf4
@@ -0,0 +1,43 @@
+package scalaz
+
+
+sealed trait HList {
+ type Wrap[M[_]] <: HList
+ type Function[B]
+ type Fold[C[_, _ <: HList] <: Up, N <: Up, Up] <: Up
+}
+
+final class HNil extends HList {
+ type Wrap[M[_]] = HNil
+ type Function[B] = B
+ def ::[T](v: T) = HCons(v, this)
+ type Fold[C[_, _ <: HList] <: Up, N <: Up, Up] = N
+ type H = Nothing
+ type T = HNil
+ def fold[C[_, _ <: HList] <: Up, N <: Up, Up](f: (H, T) => C[H, T], g: => N): Fold[C, N, Up] = g
+}
+
+final case class HCons[Head, Tail <: HList](head: Head, tail: Tail) extends HList {
+ import HList._
+
+ type Wrap[M[_]] = M[H] :: T#Wrap[M]
+
+ type Function[B] = H => T#Function[B]
+
+ def ::[T](v: T) = HCons(v, this)
+
+ type Fold[C[_, _ <: HList] <: Up, N <: Up, Up] = C[H, T]
+
+ type H = Head
+ type T = Tail
+
+ def fold[C[_, _ <: HList] <: Up, N <: Up, Up](f: (H, T) => C[H, T], g: => N): Fold[C, N, Up] = f(head, tail)
+}
+
+trait HLists {
+ val HNil = new HNil()
+
+ type ::[H, T <: HList] = HCons[H, T]
+}
+
+object HList extends HLists
@@ -22,6 +22,21 @@ sealed trait MA[M[_], A] extends PimpedType[M[A]] {
// a ⊛ b ⊛ c apply {_ + _ + _}
def ⊛[B](b: M[B]) = new ApplicativeBuilder[M, A, B](value, b)
+ import HList._
+
+ def ⊛:[B](mb: M[B]) = new ApplicativeBuilderHList[B :: A :: HNil, B :: HNil](mb :: HNil)
+
+ class ApplicativeBuilderHList[All <: HList, AllButOne <: HList](hl: AllButOne#Wrap[M]) {
+// ap(ap(ap(t.fmap(a, f.curried), b), c), d)
+ def apply[B](f: All#Function[B])(implicit func: Functor[M], ap: Apply[M]) = {
+ def apply0[H <: HList](hl0: H) = hl0.fold[Rest, HNil]((h, t) => ap(apply0(t), h), func.fmap(a, f))
+
+ apply0(hl)
+ }
+
+ def ⊛:[B](b: M[B]) = new ApplicativeBuilderHList[B :: All, B :: AllButOne](HCons(b, hl))
+ }
+
def <*>[B](f: M[A => B])(implicit a: Apply[M]): M[B] = a(f, value)
def <**>[B, C](b: M[B])(z: (A, B) => C)(implicit t: Functor[M], a: Apply[M]): M[C] = a(t.fmap(value, z.curried), b)
@@ -65,5 +65,8 @@ object ExampleApplicative {
case class Person(age: Int, name: String)
some(10) ⊛ none[String] apply Person.apply
+
+
+ (some(10) ⊛: none[String]).apply((Person.apply _).curried)
}
}

0 comments on commit bd3807e

Please sign in to comment.