Search code examples
functional-programmingcompositioncurryingramda.js

How can I specify the order of curried parameter application


I'm trying to convert the following to pointfree style: a function that partially applies a value to the transformer function add before passing in the collection to be iterated over. (Using Ramda.js)

R.compose(
  R.map,
  R.add
)(1, [1,2,3])

The problem is that R.add is arity 2, as is R.map. I want the application order to be as follows:

add(1)
map(add(1))
map(add(1), [1,2,3])
[add(1,1), add(1,2), add(1,3)]

But what happens instead is this:

add(1, [1,2,3])
map(add(1, [1,2,3]))
<partially applied map, waiting for collection>

Anyone know of a way to specify this behavior?


Solution

  • A plain compose or pipe won't do this because either will absorb all the arguments supplied into the first function. Ramda includes two additional functions that help with this, converge and useWith. In this case useWith is the one that will help:

    useWith(map, [add, identity])(1, [1, 2, 3]); //=> [2, 3, 4]
    

    While identity is not absolutely required here, it gives the generated function the correct arity.