Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Functor[T[_]]{
- def map[A,B](f:A=>B, ta:T[A]):T[B]
- }
- trait Applicative[T[_]] extends Functor[T] {
- def unit[A](a:A):T[A]
- def ap[A,B](tf:T[A=>B], ta:T[A]):T[B]
- }
- object AppList extends Applicative[List] {
- def map[A,B](f:A=>B, as:List[A]) = as.map(f)
- def unit[A](a: A) = List(a)
- def ap[A,B](fs:List[A=>B], as:List[A]) = for(f <- fs; a <- as) yield f(a)
- }
- implicit def toApplicative[A, B](fs: List[A=>B]) = new {
- def <*>(as: List[A]) = AppList.ap(fs, as)
- }
- val f2: (String, String) => String = {(first, last) => s"$first $last"}
- val firsts = List("a", "b", "c")
- val lasts = List("x", "y", "z")
- scala> AppList.pure(f2.curried) <*> firsts <*> lasts
- res31: List[String] = List(a x, a y, a z, b x, b y, b z, c x, c y, c z)
- val firstsOpt = Some(firsts)
- val lastsOpt = Some(lasts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement