Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created November 19, 2010 17:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debasishg/706816 to your computer and use it in GitHub Desktop.
Save debasishg/706816 to your computer and use it in GitHub Desktop.
// for comprehension
scala> for {
| l <- List(2,5,10)
| m <- List(8,10,11)
| } yield l * m
res17: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
// map a pure function into applicatives
scala> List(8,10,11) <*> (List(2,5,10) map (((_: Int) * (_: Int)).curried))
res18: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
// lift-2 a pure function into an applicative
scala> List(2,5,10).<**>(List(8,10,11))(_ * _)
res21: List[Int] = List(16, 20, 22, 40, 50, 55, 80, 100, 110)
@retronym
Copy link

Or

(List(8,10,11) |@| (List(2,5,10)) { _ * _ }

Which generalizes nicely to higher arities.

(List(8,10,11) |@| (List(2,5,10) |@| (List(0,2,4)) { _ * _ * _ }

@debasishg
Copy link
Author

Thanks a lot .. Looks nicer because of type inference .. I have still so much to learn in Scalaz :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment