Option Handling in Scala
Python None -> Scala Option
x: str | None->x: Option[String]if x is None->x match { case None => ... case Some(v) => ... }- Or use
.map,.flatMap,.getOrElse,.fold
Processing Pipelines with Option
Instead of mutable var + early returns:
// BAD (Python-like)
var w = word
if (condition) w = transform(w)
if (w.length < min) return None
// GOOD (functional Scala)
Some(word)
.map(w => if (condition) transform(w) else w)
.filter(_.length >= min)
.map(w => maxLen.fold(w)(m => w.take(m)))
.filter(_.nonEmpty)
Option with Collections
list.flatMap(f)wherefreturnsOptionnaturally filters NonecollectFirstfor finding first matching elementiterator.map(...).collectFirst { case Some(x) => x }for lazy first-match