Closures and Curries
Closures and currying are both important concepts in JavaScript, especially for functional programming.
Closure
A closure is a function that has access to its outer scope even after the outer function has returned. It “closes over” its outer scope, hence the name. Here’s an example:
function outerFunction(x) {
return function innerFunction(y) {
return x + y;
}
}
const add5 = outerFunction(5);
console.log(add5(3)); // 8
In the above example, **innerFunction**
has access to **x**
even after **outerFunction**
has returned. This allows us to keep state across multiple invocations of the inner function.
We can use closures to store a function with a first parameter, you can see an example here:
Curries
Currying, on the other hand, is a technique where we convert a function that takes multiple arguments into a function that takes one argument and returns another function. This allows us to partially apply arguments to a function, making it more composable and reusable. Here’s an example:
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(null, args);
} else {
return function(...moreArgs) {
return curried.apply(null, args.concat(moreArgs));
}
}
}
}
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
const add5 = curriedAdd(5);
console.log(add5(3)(2)); // 10
In the above example, we take the **add**
function and curry it with the **curry**
function. This allows us to partially apply the **5**
argument to the **add**
function, creating a new function **add5**
that takes two arguments instead of three.
const add = function (a, b) {
return a + b
}
const curriedAdd = curry(add)
console.log(curriedAdd(1)(2))
The example above of simple carry, its looks like closures but not function inside function
Ramda
Ramda it’s a funcitonal javascript functions based on curry functions
[!info] Ramda Documentation
Accepts a converging function and a list of branching functions and returns a new function.
https://ramdajs.com/docs/#curry
You can try Ramda functions here: https://ramdajs.com/repl/?v=0.28.0