question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behavior of curry and curryN

See original GitHub issue

Hello,

First of all, thanks for ramda, it’s a great tool for FP. Today, trying to make some testing I almost became mad because my tests were not executing properly. I blamed node, my testing tool and my testing environment. Then I realized the problem was with lodash.curry. Why am I here then ? Because I thought:

“Ok lodash did it wrong, let’s see if ramda does the same.”

And happens that ramda does exactly the same. I can accept that maybe the person who is wrong is me, but not a single line in the documentation mentions the behavior I have discovered.

Here is a small snippet you can test on ramda REPL

const x = curry((a,b,c) => console.log(a,b,c))
x()('b')('c')()()()()('a')
const y = curryN(3,(a,b,c) => console.log(a,b,c))
y()('b')('c')()()()('x')

As you can see I can call x and y as many times as I want if I don’t provide an argument. My expectation is that, if the function takes three parameters, the curried function is executed after being invoked 3 times.

Functions communicate their arity with the length property, so there curry can easily track function invocations and compare them with the arity. Maybe I can accept that behavior on curry, but the I want to have an escape hatch on curryN, but that is not the case.

Imagine that I have a curried function that takes an optional parameter, I can’t use ES6 defaults because then curry will not work, so I try to check default inside the function.

const x = curry((a,b,c) => {
a = a || 'default'
console.log(a,b,c)
})

This will not work either because I’ll be forced to call the function like this

//not working
x()('b','c')
// works
x(null)('b','c')

As I said, I can accept this behavior for curry, but not for curryN. And even if you want to keep it the way it is, please document this particular behavior on the documentation.

Regards

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
CrossEyecommented, Sep 10, 2017

At this moment, I don’t see a problem to allow optional parameters in a curried function

It’s not really that they’re optional that’s at issue. The problem is that when JS decided to add defaults values for parameters, it chose to not count those with defaults when reporting the function’s arity.

(first, last, middle) => `${first} ${middle || ''} ${last}}`).length //=> 3
(first, last, middle = '') => `${first} ${middle} ${last}}`).length //=> 2

So we’ve already lost the ability to curry it with just plain curry, which uses that length.

But even beyond that, it’s very hard to know what to do with such arguments given Ramda’s style of currying. We could switch to straightforward currying, and forget any calls with multiple parameters. (And there’s a fair bit to be said for this, but it’s a radical change to Ramda.) Or we could invent a second kind of placeholder to say “use the default here.” But this seems extravagant for the gain.

But mostly, I think what David said is appropriate: Optional parameters and currying are two different and incompatible solutions to a single problem.

1reaction
kedashoecommented, Sep 7, 2017

I have never seen a pattern where the first argument is optional? You can use curryN to support optional arguments at the end however, if you would like:

let f = curryN(2, (a, b, c) => {
  if (c === undefined) {
    c = 'no c';
  }
  console.log(a+', '+b+', '+c);
});

https://goo.gl/2YnmPa

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dr. Shannon Curry PROVES Amber Heard's Unexpected ...
Dr. Shannon Curry PROVES Amber Heard's TRUE Mental Illness!So today Dr. Shannon Curry who had already testified before came back to rebut Dr ......
Read more >
تويتر \ Curry On Conf على تويتر: "Software Fault Isolation (SFI), a ...
Software Fault Isolation (SFI ), a way of preventing errors or unexpected behavior in one program from affecting others, is essential, but realized...
Read more >
Warriors news: Stephen Curry calls out reporter for press ...
Warriors star Stephen Curry could not help but single out a reporter after his phone began ringing during a press conference.
Read more >
Steph Curry's unexpected response when Bob Myers asked ...
After the incident, general manager Bob Myers reportedly asked Stephen Curry whether he would be willing to take Green's place as the Warriors ......
Read more >
Stephen Curry's Magic Makes Warriors Teammate Feel Like ...
Stephen Curry dances on the court. Well, not literally. However, he is always on the move, making each step unpredictable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found