I have the following code:
val userName = users.get(userID) match {
case Some(user) => user.name
case None => "Invalid User ID"
}
Is there a more concise way of writing this?
I was wondering if there was something similar to getOrElse that would let me apply a function if it is successful (to extract the user name), or return a plain value otherwise.
Not in one method call, but in two:
Oops, apparently
Option.foldwas finally added in Scala 2.10, according to @sschaef’s answer.