Master JavaScript Currying: Boost Code Quality with Functional Design
This article explains what currying is, its advantages, and provides a generic JavaScript currying function, demonstrating how to transform multi‑parameter functions into single‑parameter chains to improve code reuse, enable lazy evaluation, and enhance overall code quality in functional programming.
JavaScript is popular for its flexibility and openness, but this also leads to tangled, redundant code, and variable scope issues. This series introduces currying, code composition, and anti‑currying to mitigate JavaScript drawbacks while embracing functional programming.
1. What Is Currying?
Currying (named after logician Haskell Brooks Curry) transforms a function that takes multiple arguments into a sequence of functions each taking a single argument, returning a new function that accepts the remaining arguments.
It converts a multi‑parameter function into a series of single‑parameter functions, each returning another function that eventually produces the final result.
In practice, you call a function with some arguments, receive a new function, and continue supplying the remaining arguments later.
Before currying: func(x, y, z); After currying: func(x)(y)(z);
2. Currying Is Common
Consider a non‑curried example that splits a string:
var splitStr = function(c, str) {
return str.split(c);
};
function myFun(x, y, z) {
var myStr = wx.cgiData.str,
retStr = '';
retStr = splitStr('-', myStr);
return retStr;
}
myFun(x1, y1, z1);Using currying, the split logic can be pre‑configured:
var split = function(c) {
return function(str) {
return str.split(c);
};
};
function myFun(x, y, z, splitFun) {
var myStr = wx.cgiData.str;
var retStr = splitFun(myStr);
}
myFun(x1, x2, x3, split('-'));Currying leverages JavaScript’s ability to pass functions as arguments and return them, similar to closures.
3. A Generic Currying Function and Its Ideas
A reusable currying helper can be written as:
function curry(func) {
var __args = [];
return function callee() {
if (arguments.length === 0) {
return func.apply(this, __args);
}
[].push.apply(__args, arguments);
return callee;
};
}Tips: The curried function should ideally accept a single argument each time, facilitating code composition.
Applying the helper to a sum function:
function sumNum() {
var _tmpNum = 0;
for (var i = 0; i < arguments.length; i++) {
_tmpNum += +arguments[i];
}
return _tmpNum;
}
var sum = curry(sumNum);
// Incrementally provide arguments
sum(num1);
sum(num2);
sum(num3, num4);
// Final call without arguments returns the result
sum(); // → 80Inside curry, the __args array stores all arguments from successive calls thanks to JavaScript closures. When a call receives no arguments, the original function is invoked with the accumulated arguments.
Two clear benefits of currying emerge:
Parameter Reuse : Each call only needs to supply the current argument; previously supplied arguments are automatically retained.
Lazy Evaluation : The actual computation is deferred until a call without arguments triggers execution, reducing redundant work and temporary variables.
The essence of currying is to compose multi‑parameter functions from single‑parameter functions, simplifying interfaces and promoting code reuse.
While the generic currying helper may not fit every scenario, understanding it enables developers to apply functional programming techniques—such as custom currying and code composition—to improve code quality in their own projects.
Tips: Future articles will cover related topics like code composition and anti‑currying.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
