0

I have my function that creates ranges:

const range = from => to => step => ....

And i want to create another function that do something with that range, but i want to use pipe.

For example i want to get the sum of the range.

const sum = ...

const getSum = pipe(
    range(),
    sum,
)

I can to the following:

  const getSum = from => to => {
       return sum(range(from)(to)(1))
    }

But can I do it with ramdajs and to be point free.

For example:

const getSum = pipe(
   range..///here,
   sum
)

pipe, sum and range are my implementations.

But i am not sure how to do it point-free style.

Note that range is function that return function for easier currying.

range(0)(10)(1) 

Thank you

More description:

Imagine i have count and split functions. This is regular function:

 const numChars = str => count(split('', str))

This is point-free style (imag

const numChars = pipe(split(''), count)

I want to the the same but with the range above

  • If your `range` is fully curried, then `pipe` will be able to pass only a single parameter to it. – Scott Sauyet Jul 03 '18 at 19:23
  • But i dont have reference to the parameter and the function: pipe(range, {here}, ...) –  Jul 04 '18 at 00:41
  • "*is function that return function for easier currying*"? I think you mean "is a curried function for easier partial application". – Bergi Jul 04 '18 at 08:52
  • Yes, correct but i dont know how to pipe with two arguments or more since getSum takes 2 arguments as well. Or maybe is better not to be point-free function –  Jul 04 '18 at 12:43

1 Answers1

2

To compose with a function taking more than one argument, use composition multiple times - although you need to use R.o instead of pipe or compose:

// sum: [Number] -> Number
// range: Number -> Number -> [Number]
// getSum: Number -> Number -> Number
const getSum = R.o(R.o(R.sum), R.range);

To supply the last argument of a curried function, the easiest Ramda solution is using a placeholder:

// stepRange: Number -> Number -> Number -> [Number]
// range: Number -> Number -> [Number]
const range = stepRange(R.__, R.__, 1);

but this only works with functions created by Ramda's own curryN or curry, not with manually curried functions. With those, you can only R.flip until you can partially apply the first argument:

// stepRange: from -> to -> step -> range
// flip(stepRange): to -> from -> step -> range
// o(flip, stepRange): from -> step -> to -> range
// flip(o(flip, stepRange)): step -> from -> to -> range
// range: from -> to -> range
const range = R.flip(R.o(R.flip, stepRange))(1);

I cannot really recommend doing this. Everything is possible, but this is unreadable. Just write out the parameters:

const getSum = R.o(R.o(R.sum), R.flip(R.o(R.flip, stepRange))(1));
// vs
const getSum = from => to => R.sum(stepRange(from)(to)(1));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375