4 ms·
I find currying rarely useable in real life. Because you rarely need a fixed parameter to a function. And when you do it's solved in a better way by storing it
by dmitriid 9y ago
I find currying rarely useable in real life. Because you rarely need a fixed parameter to a function. And when you do it's solved in a better way by storing it in a state somewhere (even global variables will do in a pinch).
The closest real world example I can think of right now is calling a webservice. Which is usually something like
const baseUrl = 'https://some.tld'
function callExternal(resourcePath, data) {
fetch(baseUrl + resourcePath, data)...
}
// and then somewhere
callExternal('/some/path', {some: data})
With currying you could do it like this:
const baseFun = function(baseUrl, resourcePath, data) {
fetch(baseUrl + resourcePath, data)...
}
const callExternal = baseFun('https://some.tld')
// ^ you "pin" the first argument to always be 'https://some.tld'
// and then somewhere
callExternal('/some/path', {some: data})
I personally have had the need to write a curried function myself maybe twice over the course of the past 17 years.
- icebraining 9y agoYes, closures can be used to achieve a similar effect to currying. That said, they only work with functions you create yourself. If baseFun came from a library, you'd have to write an extra function just to close over baseUrl. Of course, one can wonder how many times can one usefully curry with external functions, but I think that's partially a result of not having currying syntax in the first place. For example, the fact that Ruby has blocks massively influences the typical design of its libraries, when compared to Python.