-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartialFunction.js
More file actions
96 lines (73 loc) · 2.3 KB
/
partialFunction.js
File metadata and controls
96 lines (73 loc) · 2.3 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// // 1. Write a function named greet that takes two arguments and logs a greeting:
// function greet(arg1, arg2) {
// let capitalized = arg1[0].toUpperCase() + arg1.slice(1);
// console.log(capitalized + ', ' + arg2 + '!');
// }
// greet('howdy', 'Joe');
// greet('good morning', 'Sue');
// function partial(primary, arg1) {
// return function(arg2) {
// return primary(arg1, arg2);
// }
// }
// let sayHello = partial(greet, 'hello');
// sayHello('Brandon');
// let sayHi = partial(greet, 'hi');
// sayHi('Sarah');
// 1. Using partial function application implement a function, sub5, that returns the value of a number subtracted by 5.
// function subtract(a, b) {
// return a - b;
// }
// function sub5(a) {
// return subtract(a, 5);
// }
// console.log(sub5(10)); // 5
// console.log(sub5(20)); // 15
// 2. This code is a bit limited however, because we can only subtract by 5.
// Implement the makeSubN function below so that we can supply any value we want to be subtracted from a,
// and get a new function that will always subtract this value.
// function subtract(a, b) {
// return a - b;
// }
// function makeSubN(n) {
// return function(a) {
// return subtract(a, n);
// }
// }
// let sub5 = makeSubN(5);
// console.log(sub5(10)); // 5
// 3. Although the solution above is more flexible, we now want to be able to supply any operation, not just subtraction.
// Implement makePartialFunc below.
// function makePartialFunc(func, b) {
// return function(a) {
// return func(a, b);
// }
// }
// function multiply(a, b) {
// return a * b;
// }
// let multiplyBy5 = makePartialFunc(multiply, 5);
// console.log(multiplyBy5(100)); // 500
// 5. Consider the code below: Implement makeMathRollCall such that it returns a partially applied rollCall function, with the subject as 'Math'.
let subjects = {
English: ['Bob', 'Tyrone', 'Lizzy'],
Math: ['Fatima', 'Gary', 'Susan'],
Biology: ['Jack', 'Sarah', 'Tanya'],
};
function rollCall(subject, students) {
console.log(subject + ':');
students.forEach(function(student) {
console.log(student);
});
}
function makeMathRollCall() {
return function(students) {
return rollCall('Math', students);
}
}
let mathRollCall = makeMathRollCall();
mathRollCall(subjects['Math']);
// => Math:
// => Fatima
// => Gary
// => Susan