Skip to content

Instantly share code, notes, and snippets.

@mergeconflict
Created January 6, 2012 18:51
Show Gist options
  • Save mergeconflict/1571886 to your computer and use it in GitHub Desktop.
Save mergeconflict/1571886 to your computer and use it in GitHub Desktop.
category folding fail
trait CategoryFoldableV[F[_], C[_, _], A] extends FoldableV[F, C[A, A]] {
implicit def C: Category[C]
////
final def composeRight = sumr(Monoid.liftCategory)
}
trait ToFoldableV0 {
implicit def ToFoldableVUnapply[FA](v: FA)(implicit F0: Unapply[Foldable, FA]) =
new FoldableV[F0.M,F0.A] { def self = F0(v); implicit def F: Foldable[F0.M] = F0.TC }
}
trait ToFoldableV extends ToFoldableV0 {
implicit def ToFoldableV[F[_],A](v: F[A])(implicit F0: Foldable[F]) =
new FoldableV[F,A] { def self = v; implicit def F: Foldable[F] = F0 }
implicit def ToCategoryFoldableV[F[_], C[_, _], A](v: F[C[A, A]])(implicit F0: Foldable[F], C0: Category[C]) =
new CategoryFoldableV[F, C, A] { def self = v; implicit def F: Foldable[F] = F0; implicit def C: Category[C] = C0 }
////
////
}
import scalaz._, Scalaz._
object Hoho {
def main(args: Array[String]) {
val fns = List[Int => Int](1 + _, 2 + _)
val plusThree = fns.composeRight // this works
System.out.println(plusThree(1))
val fns2 = List({a: Int => Option(1 + a)}, {a: Int => Option(2 + a)})
val plusThree2 = fns2.map(Kleisli[Option, Int, Int]).composeRight // this doesn't
// value composeRight is not a member of List[scalaz.Kleisli[Option,Int,Int]]
System.out.println(plusThree2(1))
}
}
trait CategoryMonoid[F[_, _], M] extends Monoid[F[M, M]] {
implicit def F: Category[F]
def append(x: F[M, M], y: => F[M, M]): F[M, M] = F.compose(x, y)
val zero = F.id[M]
}
def liftCategory[F[_, _], M](implicit F0: Category[F]): Monoid[F[M, M]] = new CategoryMonoid[F, M] {
implicit def F: Category[F] = F0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment