‘Currying’ has the same spelling as Curry that we love.
But ‘currying’ is derived from the name Haskell Brooks Curry who was known as a mathematician and a logician.
Currying is a technique that converting multiple arguments function into a single argument functions sequence.
Like this.
func(a, b, c) -> f(a)(b)(c)
Three arguments function is converted into three functions with single argument each.
f(a) returns value and (b) takes this value as a parameter. (c) also do the same thing. Get’s (b)‘s return value and return.
//non-curried
function plusFunc(a, b, c){
console.log(a + b + c);
}
plusFunc(1, 2, 3); // 6
//curried
function plusFunc(a){
return function(b){
return function(c){
console.log(a + b + c);
}
}
}
plusFunc(1)(2)(4); // 7
Then what are the differences between Curried and Non-curried?
non-curried : Function will be immediately executed even though parameters are not passed. ? Function definition : func(a, b, c) ? Function execution : func(a) ? Execution result : func(a, undefined, undefined)
curried : Execution will be suspended if parameters are not fully passed. ? Function definition : func(a, b, c) ? Function execution : func(a) ? Execution result : func(a) waits for b parameter.
Most of the functions can be implemented without using currying. but you can expect a good effect if you use currying to improve productivity through reusability or readability.
If you see ‘echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list‘, move forward.