Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Category[~>[_, _]] { def id[A]: (A ~> A); def comp[A, B, C]: (A ~> B) => (B ~> C) => (A ~> C) }
- class Function1Category extends Category[Function1] {
- def id[A]:Function1[A,A] = x => x
- def comp[A,B,C]:(A => B) => (B => C) => (A => C) = (ab:A => B) => (bc:B => C) => ab andThen bc
- //or: def comp[A,B,C]:Function1[Function1[A,B],Function1[Function1[B,C],Function1[A,C]]] = (ab:A => B) => (bc:B => C) => ab andThen bc
- }
- type ListFn[A, B] = A => List[B]
- class ListKleisliCategory extends Category[ListFn] {
- def id[A]:Function1[A,List[A]] = x => List[A](x)
- def comp[A,B,C] = (ab:A => List[B]) => (bc:B => List[C]) => ab andThen (_ flatMap bc)
- }
- def test = {
- val lkc = new ListKleisliCategory
- val lkc1 = lkc.comp[Int,Int,Int](a => List(a))(lkc.comp[Int,Int,Int](b => List(b))(c => List(c)))
- val lkc2 = lkc.comp[Int,Int,Int](lkc.comp[Int,Int,Int](a => List(a))(b => List(b)))(c => List(c))
- }
Add Comment
Please, Sign In to add comment