forked from bittu1040/JavaScript-Coding-and-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrying.js
More file actions
44 lines (30 loc) · 1.01 KB
/
Currying.js
File metadata and controls
44 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// currying
// Currying is a function that takes one argument at a time and returns a new function expecting the next argument.
// It is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).
/*
Currying in JavaScript is a functional programming technique where a function with multiple arguments
is transformed into a sequence of nested functions, each taking a single argument.
*/
// Closure helps to transform our normal function into currying function.
function addTwoNumber(a,b){
return a+b;
}
console.log(addTwoNumber(2,3))
let add = function(x){
return function(y){
return function(z){
return x+y+z;
}
}
}
// let addbytwo = add(2)
// addbytwo(3)
console.log(add(2)(4)(6));
const getlunch = (ingredient1) =>{
return (ingredient2) => {
return (ingredient3) => {
return `${ingredient1}, ${ingredient2}, ${ingredient3}`;
}
}
}
console.log(getlunch('rice')('curd')('dal'));