What is currying?
September 04th, 2025 · 2 min read
Currying is a strategy for sharing logic in programming. With it, derived functions pre-fill arguments of a parent function and inherit the remaining parameters.
JavaScript example:
// curryfied function
function sum(a) {
return function(b) {
return a + b;
}
}
const sum2 = sum(2);
// usage
console.log(sum(2)(7)); // 9
console.log(sum2(7)); // 9
This style comes from functional programming and is very useful for mathematics.
Another example, in F#:
let volumePyramid(baseAreaFormula)(w)(l)(h) =
h * (baseAreaFormula(w)(l))/3.0
let areaRectangle(w)(l) = w * l
let areaTriangle(w)(l) = (w * l)/2.0
let areaCircle(_)(r) = Math.PI * r**2.0
let volumeRectangularBasePyramid =
volumePyramid(areaRectangle)
let volumeTriangularBasePyramid =
volumePyramid(areaTriangle)
let volumeCone =
volumePyramid(areaCircle)(0)
// usage
let v1 = volumeRectangularBasePyramid(7.0)(4.0)(11.0)
let vc = volumeCone(9.0)(12.0) // radius 9, height 12
The less a parameter varies across functions, the further to the left it should be in a curried declaration.
A
AlexandreHTRBCampinas/SP,
Brasil