Why doesn't Option have a fold method?
问题 I wonder why scala.Option doesn't have a method fold like this defined: fold(ifSome: A => B , ifNone: => B) equivalent to map(ifSome).getOrElse(ifNone) Is there no better than using map + getOrElse ? 回答1: You can do: opt foldLeft (els) ((x, y) => fun(x)) or (els /: opt) ((x,y) => fun(x)) (Both solutions will evaluate els by value, which might not be what you want. Thanks to Rex Kerr for pointing at it.) Edit: But what you really want is Scalaz’s catamorphism cata (basically a fold which not