Guest User

Untitled

a guest
Jan 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. trait Category[~>[_, _]] { def id[A]: (A ~> A); def comp[A, B, C]: (A ~> B) => (B ~> C) => (A ~> C) }
  2.  
  3. class Function1Category extends Category[Function1] {
  4. def id[A]:Function1[A,A] = x => x
  5. def comp[A,B,C]:(A => B) => (B => C) => (A => C) = (ab:A => B) => (bc:B => C) => ab andThen bc
  6. //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
  7. }
  8.  
  9. type ListFn[A, B] = A => List[B]
  10.  
  11. class ListKleisliCategory extends Category[ListFn] {
  12. def id[A]:Function1[A,List[A]] = x => List[A](x)
  13. def comp[A,B,C] = (ab:A => List[B]) => (bc:B => List[C]) => ab andThen (_ flatMap bc)
  14. }
  15.  
  16. def test = {
  17. val lkc = new ListKleisliCategory
  18. val lkc1 = lkc.comp[Int,Int,Int](a => List(a))(lkc.comp[Int,Int,Int](b => List(b))(c => List(c)))
  19. val lkc2 = lkc.comp[Int,Int,Int](lkc.comp[Int,Int,Int](a => List(a))(b => List(b)))(c => List(c))
  20. }
Add Comment
Please, Sign In to add comment