Skip to content
Permalink
Browse files

cleaning up applicative and lifting syntax - phase 1

  • Loading branch information...
pchiusano committed Aug 8, 2012
1 parent ee07c54 commit 9412258332e2dd42ecc82a363e39decb503eb2d5
@@ -24,8 +24,8 @@ trait Applicative[F[_]] extends Apply[F] with Pointed[F] { self =>
override def map[A, B](fa: F[A])(f: A => B): F[B] =
ap(fa)(point(f))

override def map2[A, B, C](fa: => F[A], fb: => F[B])(f: (A, B) => C): F[C] =
ap2(fa, fb)(point(f))
override def apply[A, B, C](fa: => F[A], fb: => F[B])(f: (A, B) => C): F[C] =
ap(fa, fb)(point(f))

// impls of sequence, traverse, etc

@@ -24,13 +24,13 @@ trait ApplicativePlus[F[_]] extends Applicative[F] with PlusEmpty[F] { self =>

def some[A](a: F[A]): F[List[A]] = {
lazy val y: Free.Trampoline[F[List[A]]] = z map (plus(_, point(Nil)))
lazy val z: Free.Trampoline[F[List[A]]] = y map (map2(a, _)(_ :: _))
lazy val z: Free.Trampoline[F[List[A]]] = y map (apply(a, _)(_ :: _))
z.run
}

def many[A](a: F[A]): F[List[A]] = {
lazy val y: Free.Trampoline[F[List[A]]] = z map (plus(_, point(Nil)))
lazy val z: Free.Trampoline[F[List[A]]] = y map (map2(a, _)(_ :: _))
lazy val z: Free.Trampoline[F[List[A]]] = y map (apply(a, _)(_ :: _))
y.run
}

@@ -29,76 +29,95 @@ trait Apply[F[_]] extends Functor[F] { self =>
def zip: Zip[F] =
new Zip[F] {
def zip[A, B](a: => F[A], b: => F[B]): F[(A, B)] =
map2(a, b)((x, y) => (x, y))
apply(a, b)((x, y) => (x, y))
}

def ap2[A,B,C](fa: => F[A], fb: => F[B])(f: F[(A,B) => C]): F[C] =
def ap[A,B,C](fa: => F[A], fb: => F[B])(f: F[(A,B) => C]): F[C] =
ap(fb)(ap(fa)(map(f)(_.curried)))
def ap3[A,B,C,D](fa: => F[A], fb: => F[B], fc: => F[C])(f: F[(A,B,C) => D]): F[D] =
ap(fc)(ap2(fa,fb)(map(f)(f => ((a:A,b:B) => (c:C) => f(a,b,c)))))
def ap4[A,B,C,D,E](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D])(f: F[(A,B,C,D) => E]): F[E] =
ap2(fc, fd)(ap2(fa,fb)(map(f)(f => ((a:A,b:B) => (c:C, d:D) => f(a,b,c,d)))))
def ap5[A,B,C,D,E,R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E])(f: F[(A,B,C,D,E) => R]): F[R] =
ap2(fd, fe)(ap3(fa,fb,fc)(map(f)(f => ((a:A,b:B,c:C) => (d:D, e:E) => f(a,b,c,d,e)))))
def ap6[A,B,C,D,E,FF, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF])(f: F[(A,B,C,D,E,FF) => R]): F[R] =
ap3(fd, fe, ff)(ap3(fa,fb,fc)(map(f)(f => ((a:A,b:B,c:C) => (d:D, e:E, ff: FF) => f(a,b,c,d,e,ff)))))
def ap7[A,B,C,D,E,FF,G,R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G])(f: F[(A,B,C,D,E,FF,G) => R]): F[R] =
ap3(fe, ff, fg)(ap4(fa,fb,fc,fd)(map(f)(f => ((a:A,b:B,c:C,d: D) => (e:E, ff: FF, g: G) => f(a,b,c,d,e,ff,g)))))
def ap8[A,B,C,D,E,FF,G,H,R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H])(f: F[(A,B,C,D,E,FF,G,H) => R]): F[R] =
ap4(fe, ff, fg, fh)(ap4(fa,fb,fc,fd)(map(f)(f => ((a:A,b:B,c:C,d: D) => (e:E, ff: FF, g: G, h: H) => f(a,b,c,d,e,ff,g,h)))))

def map2[A, B, C](fa: => F[A], fb: => F[B])(f: (A, B) => C): F[C] = ap(fb)(map(fa)(f.curried))

def ap[A,B,C,D](fa: => F[A], fb: => F[B], fc: => F[C])(f: F[(A,B,C) => D]): F[D] =
ap(fc)(ap(fa,fb)(map(f)(f => ((a:A,b:B) => (c:C) => f(a,b,c)))))
def ap[A,B,C,D,E](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D])(f: F[(A,B,C,D) => E]): F[E] =
ap(fc, fd)(ap(fa,fb)(map(f)(f => ((a:A,b:B) => (c:C, d:D) => f(a,b,c,d)))))
def ap[A,B,C,D,E,R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E])(f: F[(A,B,C,D,E) => R]): F[R] =
ap(fd, fe)(ap(fa,fb,fc)(map(f)(f => ((a:A,b:B,c:C) => (d:D, e:E) => f(a,b,c,d,e)))))
def ap[A,B,C,D,E,FF, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF])(f: F[(A,B,C,D,E,FF) => R]): F[R] =
ap(fd, fe, ff)(ap(fa,fb,fc)(map(f)(f => ((a:A,b:B,c:C) => (d:D, e:E, ff: FF) => f(a,b,c,d,e,ff)))))
def ap[A,B,C,D,E,FF,G,R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G])(f: F[(A,B,C,D,E,FF,G) => R]): F[R] =
ap(fe, ff, fg)(ap(fa,fb,fc,fd)(map(f)(f => ((a:A,b:B,c:C,d: D) => (e:E, ff: FF, g: G) => f(a,b,c,d,e,ff,g)))))
def ap[A,B,C,D,E,FF,G,H,R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H])(f: F[(A,B,C,D,E,FF,G,H) => R]): F[R] =
ap(fe, ff, fg, fh)(ap(fa,fb,fc,fd)(map(f)(f => ((a:A,b:B,c:C,d: D) => (e:E, ff: FF, g: G, h: H) => f(a,b,c,d,e,ff,g,h)))))

@deprecated("given `F: Apply[F]` use `F(a,b)(f)` instead", "7")
def map2[A, B, C](fa: => F[A], fb: => F[B])(f: (A, B) => C): F[C] =
apply(fa,fb)(f)
@deprecated("given `F: Apply[F]` use `F(a,b,c)(f)` instead", "7")
def map3[A, B, C, D](fa: => F[A], fb: => F[B], fc: => F[C])(f: (A, B, C) => D): F[D] =
map2(map2(fa, fb)((_, _)), fc)((ab, c) => f(ab._1, ab._2, c))
apply(fa,fb,fc)(f)
@deprecated("given `F: Apply[F]` use `F(a,b,c,d)(f)` instead", "7")
def map4[A, B, C, D, E](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D])(f: (A, B, C, D) => E): F[E] =
map2(map2(fa, fb)((_, _)), map2(fc, fd)((_, _)))((t, d) => f(t._1, t._2, d._1, d._2))
def map5[A, B, C, D, E, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E])(f: (A, B, C, D, E) => R): F[R] =
map2(map3(fa, fb, fc)((_, _, _)), map2(fd, fe)((_, _)))((t, t2) => f(t._1, t._2, t._3, t2._1, t2._2))
def map6[A, B, C, D, E, FF, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF])(f: (A, B, C, D, E, FF) => R): F[R] =
map2(map3(fa, fb, fc)((_, _, _)), map3(fd, fe, ff)((_, _, _)))((t, t2) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3))
def map7[A, B, C, D, E, FF, G, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G])(f: (A, B, C, D, E, FF, G) => R): F[R] =
map2(map4(fa, fb, fc, fd)((_, _, _, _)), map3(fe, ff, fg)((_, _, _)))((t, t2) => f(t._1, t._2, t._3, t._4, t2._1, t2._2, t2._3))
def map8[A, B, C, D, E, FF, G, H, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H])(f: (A, B, C, D, E, FF, G, H) => R): F[R] =
map2(map4(fa, fb, fc, fd)((_, _, _, _)), map4(fe, ff, fg, fh)((_, _, _, _)))((t, t2) => f(t._1, t._2, t._3, t._4, t2._1, t2._2, t2._3, t2._4))
def map9[A, B, C, D, E, FF, G, H, I, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
apply(fa,fb,fc,fd)(f)

def apply[A, B, C](fa: => F[A], fb: => F[B])(f: (A, B) => C): F[C] = ap(fb)(map(fa)(f.curried))

def apply[A, B, C, D](fa: => F[A], fb: => F[B], fc: => F[C])(f: (A, B, C) => D): F[D] =
apply(apply(fa, fb)((_, _)), fc)((ab, c) => f(ab._1, ab._2, c))
def apply[A, B, C, D, E](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D])(f: (A, B, C, D) => E): F[E] =
apply(apply(fa, fb)((_, _)), apply(fc, fd)((_, _)))((t, d) => f(t._1, t._2, d._1, d._2))
def apply[A, B, C, D, E, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E])(f: (A, B, C, D, E) => R): F[R] =
apply(apply(fa, fb, fc)((_, _, _)), apply(fd, fe)((_, _)))((t, t2) => f(t._1, t._2, t._3, t2._1, t2._2))
def apply[A, B, C, D, E, FF, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF])(f: (A, B, C, D, E, FF) => R): F[R] =
apply(apply(fa, fb, fc)((_, _, _)), apply(fd, fe, ff)((_, _, _)))((t, t2) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3))
def apply[A, B, C, D, E, FF, G, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G])(f: (A, B, C, D, E, FF, G) => R): F[R] =
apply(apply(fa, fb, fc, fd)((_, _, _, _)), apply(fe, ff, fg)((_, _, _)))((t, t2) => f(t._1, t._2, t._3, t._4, t2._1, t2._2, t2._3))
def apply[A, B, C, D, E, FF, G, H, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H])(f: (A, B, C, D, E, FF, G, H) => R): F[R] =
apply(apply(fa, fb, fc, fd)((_, _, _, _)), apply(fe, ff, fg, fh)((_, _, _, _)))((t, t2) => f(t._1, t._2, t._3, t._4, t2._1, t2._2, t2._3, t2._4))
def apply[A, B, C, D, E, FF, G, H, I, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H], fi: => F[I])(f: (A, B, C, D, E, FF, G, H, I) => R): F[R] =
map3(map3(fa, fb, fc)((_, _, _)), map3(fd, fe, ff)((_, _, _)), map3(fg, fh, fi)((_, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3, t3._1, t3._2, t3._3))
def map10[A, B, C, D, E, FF, G, H, I, J, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
apply(apply(fa, fb, fc)((_, _, _)), apply(fd, fe, ff)((_, _, _)), apply(fg, fh, fi)((_, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3, t3._1, t3._2, t3._3))
def apply[A, B, C, D, E, FF, G, H, I, J, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H],
fi: => F[I], fj: => F[J])(f: (A, B, C, D, E, FF, G, H, I, J) => R): F[R] =
map3(map3(fa, fb, fc)((_, _, _)), map3(fd, fe, ff)((_, _, _)), map4(fg, fh, fi, fj)((_, _, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3, t3._1, t3._2, t3._3, t3._4))
def map11[A, B, C, D, E, FF, G, H, I, J, K, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
apply(apply(fa, fb, fc)((_, _, _)), apply(fd, fe, ff)((_, _, _)), apply(fg, fh, fi, fj)((_, _, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3, t3._1, t3._2, t3._3, t3._4))
def apply[A, B, C, D, E, FF, G, H, I, J, K, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H],
fi: => F[I], fj: => F[J], fk: => F[K])(f: (A, B, C, D, E, FF, G, H, I, J, K) => R): F[R] =
map3(map3(fa, fb, fc)((_, _, _)), map4(fd, fe, ff, fg)((_, _, _, _)), map4(fh, fi, fj, fk)((_, _, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3, t2._4, t3._1, t3._2, t3._3, t3._4))
def map12[A, B, C, D, E, FF, G, H, I, J, K, L, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
apply(apply(fa, fb, fc)((_, _, _)), apply(fd, fe, ff, fg)((_, _, _, _)), apply(fh, fi, fj, fk)((_, _, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t2._1, t2._2, t2._3, t2._4, t3._1, t3._2, t3._3, t3._4))
def apply[A, B, C, D, E, FF, G, H, I, J, K, L, R](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D],
fe: => F[E], ff: => F[FF], fg: => F[G], fh: => F[H],
fi: => F[I], fj: => F[J], fk: => F[K], fl: => F[L])(f: (A, B, C, D, E, FF, G, H, I, J, K, L) => R): F[R] =
map3(map4(fa, fb, fc, fd)((_, _, _, _)), map4(fe, ff, fg, fh)((_, _, _, _)), map4(fi, fj, fk, fl)((_, _, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t._4, t2._1, t2._2, t2._3, t2._4, t3._1, t3._2, t3._3, t3._4))
apply(apply(fa, fb, fc, fd)((_, _, _, _)), apply(fe, ff, fg, fh)((_, _, _, _)), apply(fi, fj, fk, fl)((_, _, _, _)))((t, t2, t3) => f(t._1, t._2, t._3, t._4, t2._1, t2._2, t2._3, t2._4, t3._1, t3._2, t3._3, t3._4))

def tuple[A,B](fa: => F[A], fb: => F[B]): F[(A,B)] =
apply(fa, fb)((_,_))
def tuple[A,B,C](fa: => F[A], fb: => F[B], fc: F[C]): F[(A,B,C)] =
apply(fa, fb, fc)((_,_,_))
def tuple[A,B,C,D](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D]): F[(A,B,C,D)] =
apply(fa, fb, fc, fd)((_,_,_,_))
def tuple[A,B,C,D,E](fa: => F[A], fb: => F[B], fc: => F[C], fd: => F[D], fe: => F[E]): F[(A,B,C,D,E)] =
apply(fa, fb, fc, fd, fe)((_,_,_,_,_))

def lift2[A, B, C](f: (A, B) => C): (F[A], F[B]) => F[C] =
map2(_, _)(f)
apply(_, _)(f)
def lift3[A, B, C, D](f: (A, B, C) => D): (F[A], F[B], F[C]) => F[D] =
map3(_, _, _)(f)
apply(_, _, _)(f)
def lift4[A, B, C, D, E](f: (A, B, C, D) => E): (F[A], F[B], F[C], F[D]) => F[E] =
map4(_, _, _, _)(f)
apply(_, _, _, _)(f)
def lift5[A, B, C, D, E, R](f: (A, B, C, D, E) => R): (F[A], F[B], F[C], F[D], F[E]) => F[R] =
map5(_, _, _, _, _)(f)
apply(_, _, _, _, _)(f)
def lift6[A, B, C, D, E, FF, R](f: (A, B, C, D, E, FF) => R): (F[A], F[B], F[C], F[D], F[E], F[FF]) => F[R] =
map6(_, _, _, _, _, _)(f)
apply(_, _, _, _, _, _)(f)
def lift7[A, B, C, D, E, FF, G, R](f: (A, B, C, D, E, FF, G) => R): (F[A], F[B], F[C], F[D], F[E], F[FF], F[G]) => F[R] =
map7(_, _, _, _, _, _, _)(f)
apply(_, _, _, _, _, _, _)(f)
def lift8[A, B, C, D, E, FF, G, H, R](f: (A, B, C, D, E, FF, G, H) => R): (F[A], F[B], F[C], F[D], F[E], F[FF], F[G], F[H]) => F[R] =
map8(_, _, _, _, _, _, _, _)(f)
apply(_, _, _, _, _, _, _, _)(f)
def lift9[A, B, C, D, E, FF, G, H, I, R](f: (A, B, C, D, E, FF, G, H, I) => R): (F[A], F[B], F[C], F[D], F[E], F[FF], F[G], F[H], F[I]) => F[R] =
map9(_, _, _, _, _, _, _, _, _)(f)
apply(_, _, _, _, _, _, _, _, _)(f)
def lift10[A, B, C, D, E, FF, G, H, I, J, R](f: (A, B, C, D, E, FF, G, H, I, J) => R): (F[A], F[B], F[C], F[D], F[E], F[FF], F[G], F[H], F[I], F[J]) => F[R] =
map10(_, _, _, _, _, _, _, _, _, _)(f)
apply(_, _, _, _, _, _, _, _, _, _)(f)
def lift11[A, B, C, D, E, FF, G, H, I, J, K, R](f: (A, B, C, D, E, FF, G, H, I, J, K) => R): (F[A], F[B], F[C], F[D], F[E], F[FF], F[G], F[H], F[I], F[J], F[K]) => F[R] =
map11(_, _, _, _, _, _, _, _, _, _, _)(f)
apply(_, _, _, _, _, _, _, _, _, _, _)(f)
def lift12[A, B, C, D, E, FF, G, H, I, J, K, L, R](f: (A, B, C, D, E, FF, G, H, I, J, K, L) => R): (F[A], F[B], F[C], F[D], F[E], F[FF], F[G], F[H], F[I], F[J], F[K], F[L]) => F[R] =
map12(_, _, _, _, _, _, _, _, _, _, _, _)(f)
apply(_, _, _, _, _, _, _, _, _, _, _, _)(f)

////
val applySyntax = new scalaz.syntax.ApplySyntax[F] {}
@@ -5,7 +5,7 @@ private[scalaz] trait CompositionFunctor[F[_], G[_]] extends Functor[({type λ[

implicit def G: Functor[G]

override def map[A, B](fga: F[G[A]])(f: (A) => B): F[G[B]] = F.map(fga)(ga => G.map(ga)(f))
override def map[A, B](fga: F[G[A]])(f: (A) => B): F[G[B]] = F(fga)(ga => G(ga)(f))
}

private[scalaz] trait CompositionPointed[F[_], G[_]] extends Pointed[({type λ[α] = F[G[α]]})#λ] with CompositionFunctor[F, G] {
@@ -22,7 +22,7 @@ private[scalaz] trait CompositionApply[F[_], G[_]] extends Apply[({type λ[α] =
implicit def G: Apply[G]

def ap[A, B](fa: => F[G[A]])(f: => F[G[A => B]]): F[G[B]] =
F.map2(f, fa)((ff, ga) => G.ap(ga)(ff))
F(f, fa)((ff, ga) => G.ap(ga)(ff))
}

private[scalaz] trait CompositionApplicative[F[_], G[_]] extends Applicative[({type λ[α] = F[G[α]]})#λ] with CompositionPointed[F, G] with CompositionFunctor[F, G] {
@@ -31,7 +31,7 @@ private[scalaz] trait CompositionApplicative[F[_], G[_]] extends Applicative[({t
implicit def G: Applicative[G]

def ap[A, B](fa: => F[G[A]])(f: => F[G[A => B]]): F[G[B]] =
F.map2(f, fa)((ff, ga) => G.ap(ga)(ff))
F(f, fa)((ff, ga) => G.ap(ga)(ff))
}

private[scalaz] trait CompositionApplicativePlus[F[_], G[_]] extends ApplicativePlus[({type λ[α] = F[G[α]]})#λ] with CompositionPointed[F, G] with CompositionFunctor[F, G] with CompositionApplicative[F, G] {
@@ -41,7 +41,7 @@ private[scalaz] trait CompositionApplicativePlus[F[_], G[_]] extends Applicative

def empty[A]: F[G[A]] = F.empty[G[A]]
def plus[A](a: F[G[A]], b: => F[G[A]]): F[G[A]] =
F.map2(a, b)(G.plus(_, _))
F(a, b)(G.plus(_, _))
}

private[scalaz] trait CompositionFoldable[F[_], G[_]] extends Foldable[({type λ[α] = F[G[α]]})#λ] {
@@ -76,8 +76,7 @@ private[scalaz] trait CompositionDistributive[F[_], G[_]] extends Distributive[(
implicit def G: Distributive[G]

def distributeImpl[X[_]:Functor, A, B](a: X[A])(f: A => F[G[B]]): F[G[X[B]]] =
F.map(F.distribute(a)(f))(G.cosequence(_))

F(F.distribute(a)(f))(G.cosequence(_))
}

private[scalaz] trait CompositionZip[F[_], G[_]] extends Zip[({type λ[α] = F[G[α]]})#λ] {
@@ -5,7 +5,7 @@ package scalaz
* Functors, covariant by nature if not by Scala type. Their key
* operation is `map`, whose behavior is constrained only by type and
* the functor laws.
*
*
* Many useful functors also have natural [[scalaz.Apply]] or
* [[scalaz.Bind]] operations. Many also support
* [[scalaz.Traverse]].
@@ -21,8 +21,11 @@ trait Functor[F[_]] { self =>

// derived functions

/** Alias for `map`. */
def apply[A, B](fa: F[A])(f: A => B): F[B] = map(fa)(f)

/** Lift `f` into `F`. */
def apply[A, B](f: A => B): F[A] => F[B] = map(_)(f)
def lift1[A, B](f: A => B): F[A] => F[B] = map(_)(f)

/** Inject `a` to the left of `B`s in `f`. */
def strengthL[A, B](a: A, f: F[B]): F[(A, B)] = map(f)(b => (a, b))
@@ -37,7 +37,7 @@ trait IdInstances {

// Overrides for efficiency.

override def apply[A, B](f: (A) => B): Id[A] => Id[B] = f
override def lift1[A, B](f: (A) => B): Id[A] => Id[B] = f

// `ffa: Id[Id[A]]`, gives, "cyclic aliasing or subtyping involving type Id", but `ffa: A` is identical.
override def join[A](ffa: A) = ffa
@@ -53,7 +53,7 @@ trait IndexedSeqSubInstances extends IndexedSeqInstances0 with IndexedSeqSub {se

def traverseImpl[F[_], A, B](v: IxSq[A])(f: A => F[B])(implicit F: Applicative[F]) = {
DList.fromList(v.toList).foldr(F.point(empty[B])) {
(a, fbs) => F.map2(f(a), fbs)(_ +: _)
(a, fbs) => F(f(a), fbs)(_ +: _)
}
}

@@ -48,7 +48,7 @@ trait ListInstances extends ListInstances0 {
// }

DList.fromList(l).foldr(F.point(List[B]())) {
(a, fbs) => F.map2(f(a), fbs)(_ :: _)
(a, fbs) => F(f(a), fbs)(_ :: _)
}
}

@@ -14,7 +14,7 @@ trait SetInstances {
// TODO duplication with ListInstances
def traverseImpl[F[_], A, B](l: Set[A])(f: A => F[B])(implicit F: Applicative[F]) = {
DList.fromList(l.toList).foldr(F.point(Set[B]())) {
(a, fbs) => F.map2(f(a), fbs)((a, b) => b + a)
(a, fbs) => F(f(a), fbs)((a, b) => b + a)
}
}

@@ -9,7 +9,7 @@ trait StreamInstances {
val seed: G[Stream[B]] = G.point(Stream[B]())

foldRight(fa, seed) {
(x, ys) => G.map2(f(x), ys)((b, bs) => b #:: bs)
(x, ys) => G(f(x), ys)((b, bs) => b #:: bs)
}
}

@@ -50,7 +50,7 @@ trait StreamInstances {
*
* Example:
* {{{
* streamZipApplicative.map2(Stream(1, 2), Stream(3, 4))(_ * _) // Stream(3, 8)
* streamZipApplicative(Stream(1, 2), Stream(3, 4))(_ * _) // Stream(3, 8)
* }}}
*/
implicit val streamZipApplicative: Applicative[({type λ[α]=Stream[α] @@ Zip})#λ] = new Applicative[({type λ[α]=Stream[α] @@ Zip})#λ] {
@@ -127,7 +127,7 @@ trait StreamFunctions {
final def unfoldForestM[A, B, M[_] : Monad](as: Stream[A])(f: A => M[(B, Stream[A])]): M[Stream[Tree[B]]] = {
def mapM[T, U](ts: Stream[T], f: T => M[U]): M[Stream[U]] =
ts.foldRight[M[Stream[U]]](Monad[M].point(scala.Stream())) {
case (g, h) => Monad[M].map2(f(g), h)(_ #:: _)
case (g, h) => Monad[M].apply(f(g), h)(_ #:: _)
}

def unfoldTreeM(v: A) =
@@ -6,7 +6,7 @@ trait TupleInstances0 {
override def bimap[A, B, C, D](fab: (A, B))(f: (A) => C, g: (B) => D) =
(f(fab._1), g(fab._2))
def bitraverseImpl[G[_]: Applicative, A, B, C, D](fab: (A, B))(f: (A) => G[C], g: (B) => G[D]) =
Applicative[G].map2(f(fab._1), g(fab._2))((_, _))
Applicative[G].apply(f(fab._1), g(fab._2))((_, _))
}

implicit def tuple1Semigroup[A1](implicit A1: Semigroup[A1]) = new Tuple1Semigroup[A1] {
@@ -12,7 +12,7 @@ trait MapInstances {

def bitraverseImpl[G[_]: Applicative, A, B, C, D](fab: Entry[A, B])
(f: (A) => G[C], g: (B) => G[D]) =
Applicative[G].map2(f(fab.getKey), g(fab.getValue))(new SimpleImmutableEntry(_, _))
Applicative[G].apply(f(fab.getKey), g(fab.getValue))(new SimpleImmutableEntry(_, _))

}
}

0 comments on commit 9412258

Please sign in to comment.
You can’t perform that action at this time.