Created
October 28, 2011 10:35
-
-
Save notyy/1322045 to your computer and use it in GitHub Desktop.
flatmap
This file contains 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 toInt(in: String): Option[Int] = | |
try { | |
Some(Integer.parseInt(in.trim)) | |
} catch { | |
case e: NumberFormatException => None | |
} | |
def sum(in: Seq[String]) = { | |
val ints = in.flatMap(s => toInt(s)) | |
ints.foldLeft(0)((a, b) => a + b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment