Shortcuts for option and list matching in Scala
Instead of:
option match
{
case None => 0
case Some(value) => value.intField
}
you can write:
option.map(.intField).getOrElse(0)Instead of:
list match
{
case1 Nil => 0
case list => list.reduceLeft((a,b)=>a+b)
}
you can write:
list.foldLeft(0)((a,b)=>a+b)
Labels: scala
