Created
January 10, 2016 21:11
-
-
Save timvw/1b97d3bde09e6bddf8b6 to your computer and use it in GitHub Desktop.
map/flatmap/filter implemented as for-expression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Demo { | |
| def map[A, B](xs: List[A], f: A => B): List[B] = | |
| for (x <- xs) yield f(x) | |
| def flatMap[A, B](xs: List[A], f: A => List[B]): List[B] = | |
| for (x <- xs; y <- f(x)) yield y | |
| def filter[A](xs: List[A], p: A => Boolean): List[A] = | |
| for (x <- xs if p(x)) yield x | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment