Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Option.fold - why is its second argument not a binary operator?

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 and z

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 this scala.Option's value if the scala.Option is nonempty. Otherwise, evaluates expression ifEmpty.

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?

like image 948
The Archetypal Paul Avatar asked Nov 01 '22 15:11

The Archetypal Paul


1 Answers

Why should it be?

The reason fold "normally" takes a binary operator is because, "normally" is used over a list that can be processed one by one with binary operators (and the seed value z).

Fold on an option is, de facto, a map function with a default case. If you look at its definition, it literally is:

if (isEmpty) ifEmpty else f(this.get)

Since you only have one possible argument, f has to be an unary operator. One could argue to have an option fold that takes a binary operator and used the ifEmpty value as the seed in case the option is defined, but your sensible seed value and your sensible value for when the option is empty may be greatly different.

As someone pointed out, for different structures you need different arities (such as for trees), because the "sensible" application of a reduction has different structures.

like image 194
Diego Martinoia Avatar answered Nov 10 '22 10:11

Diego Martinoia