Scala: short form of pattern matching that returns Boolean

This is exactly why I wrote these functions, which are apparently impressively obscure since nobody has mentioned them.

scala> import PartialFunction._
import PartialFunction._

scala> cond("abc") { case "def" => true }
res0: Boolean = false

scala> condOpt("abc") { case x if x.length == 3 => x + x }
res1: Option[java.lang.String] = Some(abcabc)

scala> condOpt("abc") { case x if x.length == 4 => x + x }
res2: Option[java.lang.String] = None

Leave a Comment

tech