Created
October 2, 2014 20:26
-
-
Save bbq2100/2f88eb0c034d5c7034c3 to your computer and use it in GitHub Desktop.
Scala-Map function => pattern matching vs for-comprehension J
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
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