Skip to content

Instantly share code, notes, and snippets.

@timvw
Created January 10, 2016 21:11
Show Gist options
  • Select an option

  • Save timvw/1b97d3bde09e6bddf8b6 to your computer and use it in GitHub Desktop.

Select an option

Save timvw/1b97d3bde09e6bddf8b6 to your computer and use it in GitHub Desktop.
map/flatmap/filter implemented as for-expression
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