Skip to content

Instantly share code, notes, and snippets.

@bbq2100
Created October 2, 2014 20:26
Show Gist options
  • Save bbq2100/2f88eb0c034d5c7034c3 to your computer and use it in GitHub Desktop.
Save bbq2100/2f88eb0c034d5c7034c3 to your computer and use it in GitHub Desktop.
Scala-Map function => pattern matching vs for-comprehension J
def mapPatternMatching[A, B](xs: List[A], f: A => B): List[B] = {
xs match {
case List() => scala.Nil
case head :: tail => f(head) :: mapPatternMatching(tail, f)
}
}
//map(List(1, 2, 3), (x: Int) => x + 1)
def mapForComprehension[A, B](xs: List[A], f: A => B) = {
for(x <- xs) yield f(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment