Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. trait Functor[T[_]]{
  2. def map[A,B](f:A=>B, ta:T[A]):T[B]
  3. }
  4.  
  5. trait Applicative[T[_]] extends Functor[T] {
  6. def unit[A](a:A):T[A]
  7. def ap[A,B](tf:T[A=>B], ta:T[A]):T[B]
  8. }
  9.  
  10. object AppList extends Applicative[List] {
  11. def map[A,B](f:A=>B, as:List[A]) = as.map(f)
  12. def unit[A](a: A) = List(a)
  13. def ap[A,B](fs:List[A=>B], as:List[A]) = for(f <- fs; a <- as) yield f(a)
  14. }
  15.  
  16. implicit def toApplicative[A, B](fs: List[A=>B]) = new {
  17. def <*>(as: List[A]) = AppList.ap(fs, as)
  18. }
  19.  
  20. val f2: (String, String) => String = {(first, last) => s"$first $last"}
  21. val firsts = List("a", "b", "c")
  22. val lasts = List("x", "y", "z")
  23.  
  24. scala> AppList.pure(f2.curried) <*> firsts <*> lasts
  25. res31: List[String] = List(a x, a y, a z, b x, b y, b z, c x, c y, c z)
  26.  
  27. val firstsOpt = Some(firsts)
  28. val lastsOpt = Some(lasts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement