-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter5-Functional.html
148 lines (119 loc) · 2.66 KB
/
chapter5-Functional.html
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<html>
<body>
<script type="text/javascript">
// High-Order Functions
function printArray(array) {
for (var i=0;i<array.length;i++) {
print(array[i]);
}
}
function forEach(array,action) {
for (var i=0;i<array.length;i++) {
action(array[i]);
}
}
// Anonymous Functions
function sum(numbers) {
var total=0;
forEach(numbers, function(number) {
total += number;
});
return total;
}
//Rewrite Cat-Finding Code from Chapter 3
forEach(mailArchive[mail].split("\n") , handleParagraph);
//Modifying Functions
function negate(func) {
return function(x) {
return !func(x);
};
}
function negate(func) {
return function() {
return !func.apply(null, arguments);
};
}
// The reduce Function
function reduce(combine, base, array) {
forEach(array, function(element) {
base = combine(base, element);
});
return base;
}
function add(a, b) {
return a+b;
}
function sum(numbers) {
return reduce(add, 0, numbers);
}
function countZeroes(array) {
function counter(total, element) {
return total + (element === 0 ? 1 : 0);
}
return reduce(counter, 0, array);
}
function count(test, array) {
var counted=0;
forEach(array, function(element) {
if (test(element)) counted++;
});
return counted;
}
function countZeroes(array) {
function isZero(x) {return x === 0;}
return count(isZero, array);
}
// Map
funciton map(func, array) {
var result=[];
forEach(array, function(element) {
result.push(func(element));
});
return result;
}
// Other Functional Tricks
//Operator functions
var op= {
"+": function(a, b) {return a+b;},
"==": function(a,b) {return a==b;},
"===": function(a,b) {return a===b;},
"!": function(a){return !a;}
/* and so on */
};
//Partial Application
function partial(func) {
var knownArgs=arguments;
return function() {
var realArgs=[];
for (var i=1;i<knownArgs.length;i++)
realArgs.push(knownArgs[i]);
for (var i=0;i<arguments.length;i++)
realArgs.push(arguments[i]);
return func.apply(null,readings);
};
}
//example of Map and Partial
map(partial(op["+"],1), [0,2,4,6,8,10]);
function square(x) {
return x*x;
}
map(partial(map,square), [[10], [0, 1], [3]]);
// Composition
function negate(func) {
return function() {
return function() {
return !func.apply(null, arguments);
}
}
}
function compose(f1,f2) {
return function() {
return f1(f2.apply(null, arguments));
}
}
//example of composition
var isNotNaN=compose(op["1"], isNaN);
isNotNan(5);
</script>
</body>
</html>