-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintroToCurrying&PatialApplication.js
More file actions
51 lines (40 loc) · 1.58 KB
/
introToCurrying&PatialApplication.js
File metadata and controls
51 lines (40 loc) · 1.58 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
45
46
47
48
49
50
51
// Functional Programming: Introduction to Currying and Partial Application
// The arity of a function is the number of arguments it requires.
// Currying a function means to convert a function of N arity into N functions of arity 1.
// In other words, it restructures a function so it takes one argument, then returns another function that takes the next argument, and so on.
// Here's an example:
//Un-curried function
function unCurried(x, y) {
return x + y;
}
//Curried function
function curried(x) {
return function(y) {
return x + y;
}
}
curried(1)(2) // Returns 3
//This is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available. Here's an example using the curried function in the example above:
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
//Similarly, partial application can be described as applying a few arguments to a function at a time
//and returning another function that is applied to more arguments.
//Here's an example:
//Impartial function
function impartial(x, y, z) {
return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
//Fill in the body of the add function so it uses currying to add parameters x, y, and z.
function add(x) {
// Add your code below this line
return function(y){
return function(z){
return x+y+z;
}
}
// Add your code above this line
}
add(10)(20)(30);