scala - Why doesn't Option have a fold method? -


I wonder why scala.option has no way fold defined as:

  fold (IfSome: A => B, ifNone: = & gt; B)   

equals Map (ifSome) .getOrElse (ifNone)

What map + getOrElse ?

You can do this:

  opt foldLeft (els) (X, y) => funny (x))   

or

  (ELS /: OPT) ((x, y) = > Fun (X))   

(Both will evaluate the solution value els ), which may be What do you want To indicate this.)

Edit:

But what exactly do you want Scalazâ € ™ s cata (basically a fold which handles not only some values, but also does not map to any part (Some: A => X, None: => X): X = Value Match {Case No => Any case is anything (a) => Some (A)}

which is equal to opt.map (some) .getOrElse (none) .

However, I should comment that you should use only CAT when it is more natural ???? The way to express it is like many cases where a simple map â getOrElse is sufficient, especially when there are many maps (though You can sort the fun s with the function structure, of course, it depends on whether you want to focus on the function structure or value change.)

Comments