4 ms·
Nice explanation, however I don't really see the use case for currying yet. If you want functions with fixed arguments, you could also just create a new functio
by leodriesch 5y ago
Nice explanation, however I don't really see the use case for currying yet. If you want functions with fixed arguments, you could also just create a new function that calls the other with a fixed first argument and just takes the other arguments. Am I missing something?
- goto11 5y agoWhat you describe is currying.
- jhgb 5y agoIsn't that more like partial application?
- schpaencoder 5y agoAt that moment, Anton became enlightened.
- deleted 5y ago[deleted]
- Normal_gaussian 5y agoWith the main benefit of being able to also transform the argument orser or output value at the same time.
- graftak 5y agoThe most common case I’ve seen is when you want to map using 'point free' style functions, i.e. “x.map(transformItem)”[1]. Another (more) common scenario—but not yet relevant in JavaScript—is piping/composing functions[2]. [1] https://en.wikipedia.org/wiki/Tacit_programming https://en.wikipedia.org/wiki/Tacit_programming [2] https://github.com/tc39/proposal-pipeline-operator/wiki https://github.com/tc39/proposal-pipeline-operator/wiki
- ttiurani 5y agoWhat you're missing is that the curry() function is generic: you don't need to manually write that new function, but just type a simple oneliner using curry() to generate the function you're describing.
- leodriesch 5y agoDefining one function that just calls another with a fixed argument is also a one-liner with arrow functions. And I would argue that this would be easier to understand and anyone reading the code wouldn’t have to understand what currying is.
- ttiurani 5y agoI agree that the benefit is miniscule even when both the writer and the readers are familiar with currying. Which is why I personally never use curry methods to keep the cognite load low, and instead do just that.
- latexr 5y ago> If you want functions with fixed arguments, you could also just create a new function that calls the other with a fixed first argument and just takes the other arguments. From searching for practical applications a while back, I understood currying as a way to create a new function with fixed values rather than fixed arguments. Meaning if you had a function take arguments FIRST and SECOND, with FIRST being computationally expensive, currying would allow you to create a new function with the slower part pre-calculated. But after brief testing I’m unsure that’s the case. It seems the examples might’ve been referring to the argument itself being a computationally expensive operation, in which case—and going back to your example—you could save the computation’s result to a variable beforehand and use that.
- fendy3002 5y agoAlso useful in react. It's better to use `data-props` for performance, but if somehow `data-` props cannot contain the information (function maybe), you can use currying with previously memoize it first. For example, if you want to pass an object to onClick event: const handleOnClick = _.memoize( (myObj) => (evt) => { // you can do something with myObj here }); // on render return <button onClick={handleOnClick(myObj)} >Try it</button>; Memoize works on class component but I don't know if it works on function component.