I'm sure there's a good reason for this, but I'm not seeing it.
Fold
on (say) List
returns
the result of applying fold operator
op
between all the elements andz
It has an obvious relationship with foldLeft
and foldRight
that do the same thing but with defined order (and so don't need associative operators)
Fold
on Option
returns
Returns the result of applying
f
to thisscala.Option
's value if thescala.Option
is nonempty. Otherwise, evaluates expressionifEmpty
.
ifEmpty
is (in the position of) the z
for a List. f
is (in the position of) the op
For None
(which, using my mental model of Option
as a "container" that may or may not contain a value, is an "empty" container), things are OK, Option.fold
returns the zero (value of ifEmpty
).
For Some(x)
, though, shouldn't f
take two params, z
and x
so it's consistent with the fold
on sequences (including having a similar relationship to foldLeft
and foldRight
).
There's definitely a utility argument against this - having f
just take x
as a parameter in practice probably more convenient. In most cases, if it did also take z
that would be ignored. But consistency matters too...
So can someone explain to me why fold
on Option is still a "proper" fold
?