Skip to content

Instantly share code, notes, and snippets.

@notyy
Created October 28, 2011 10:35
Show Gist options
  • Save notyy/1322045 to your computer and use it in GitHub Desktop.
Save notyy/1322045 to your computer and use it in GitHub Desktop.
flatmap
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