paldepind / union-type

A small JavaScript library for defining and using union types.
MIT License
477 stars 28 forks source link

Should caseOn be flipped? #47

Closed JAForbes closed 6 years ago

JAForbes commented 8 years ago

In order to use caseOn as a reducer it would be more convenient to have caseOn parameter order reversed.

I noticed in the functional frontend architecture that caseOn is flipped there as well.

I realise this is a breaking change, but I wonder if there some way to solve this, or if there was some reason the arguments were in the other order in the first place.

paldepind commented 8 years ago

Maybe. I'm not sure. caseOn should definitely work as a reducer function. But the problem is whether or not a reducer function takes it's accumulator first or last. When doing functional programming I believe a -> b -> b, i.e. the accumulator last, is most convenient. However that is not the typical order in JavaScript. So maybe is should be changed.

JAForbes commented 8 years ago

@paldepind That is interesting. Why would accumulator last be more convenient? I am trying to imagine the ramifications of that.

roobie commented 8 years ago

I would argue for that having the accumulator last supports currying in a better fashion.

davidchambers commented 8 years ago

For what it's worth Haskell's foldl is of the following type:

foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
paldepind commented 8 years ago

Exactly. The other argument order is much better for currying, where functions typically take the structure to modify as the last element.

@davidchambers, in Haskell there is foldr which has the "correct" order. Also, foldr is more common as far as I'm aware.