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


I'm sure there is a good reason for this, but I'm not seeing it.

Apply code between the elements turn on (say) list returns

z

with a foldLeft and foldRight The obvious relationship is that those who do the same thing are defined but the order (and therefore the companionship operators are not required)

return on the option returns

Returns the result of f in the value of this scala.option Second if scala.Option nonempty otherwise, the word ifEmpty .

ifEmpty , evaluates in the status of z for the list. is any for f op

(which is using the Option which can not be included in the form of a "container", there is an "empty" container), things are ok, option.fold zero (). For

some (x) , however, f should not take two parameters, z and X as it is corresponding to fold on sequences (in which foldLeft and foldRight ).

Of course, there is a utility argument against this - it may be possible that to carry f bus x as a parameter Be more convenient. In most cases, if he has also taken z , then it will be ignored. But in the case of stability ...

So can someone explain to me why the option fold still a "fair" fold ?

Why should it be?

The reason "normally" takes a binary operator because, "normally" is used on top of a list which is processed one by one with binary operators (and seed value z) can go.

Turn on one option, in fact, there is a map function with the default case. If you look at its definition, it literally means:

  if (below If this is the only argument possible, f should be an ani operator. One chooses an option that takes a binary operator and if the option is defined then uses the ifEmpty value as the Bid, but your sensible value can be very different when the value and option of your sensible seed is empty . 

As someone has said, for different structures, you need different stages (like trees), because there are different structures in the "sensible" application to reduce.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -