functionaljs / functional-js

A functional JavaScript library that facilitates currying and point-free programming
222 stars 14 forks source link

Currying increasing arity... #6

Open hoegrammer opened 8 years ago

hoegrammer commented 8 years ago

I like this and I'm thinking of using it in my next project but I'm struggling to understand why currying increases arity and whether this is a good idea. Is it taken from another functional language? The only one I am familiar with is Haskell which does not do that.

When I try currying a function with 3 inputs and then increasing the arity, I get strange results:

addAndMult = fjs.curry((x, y, z) => (x + y) * z addAndMult (2, 6, 3) 24 // as expected: 8 times 3 addAndMult(2, 6, 3, 1) 0 // why? addAndMult(2, 6, 3, 1, 2) 2 // why? addAndMult(2, 6, 3, 1, 2, 3) 10 // why?

leecrossley commented 7 years ago

Extending arity is really designed for an initial 2 defined arguments in your base function, i.e.

fjs.curry((x, y) => { return x + y; });

Imagine it's passing the result back in as the first arg, then the next as the second - like a fold.

I should probably document and implement this better.

lambdatastic commented 7 years ago

This doesn't really match the mathematical definition of currying. The functionality you've added to is is a combination of unapply and reduce from Ramda. Using that library and the addition function you used above, it would look more like this:

R.unapply(R.reduce((x, y) => (x + y), 0))