forked from funjs/book-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter06.js
272 lines (215 loc) · 5.23 KB
/
chapter06.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
function myLength(ary) {
if (_.isEmpty(ary))
return 0;
else
return 1 + myLength(_.rest(ary));
}
function cycle(times, ary) {
if (times <= 0)
return [];
else
return cat(ary, cycle(times - 1, ary));
}
var zipped1 = [['a', 1]];
function constructPair(pair, rests) {
return [construct(_.first(pair), _.first(rests)),
construct(second(pair), second(rests))];
}
constructPair(['a', 1],
constructPair(['b', 2],
constructPair(['c', 3], [[],[]])));
//=> [['a','b','c'],[1,2,3]]
function unzip(pairs) {
if (_.isEmpty(pairs)) return [[],[]];
return constructPair(_.first(pairs), unzip(_.rest(pairs)));
}
var influences = [
['Lisp', 'Smalltalk'],
['Lisp', 'Scheme'],
['Smalltalk', 'Self'],
['Scheme', 'JavaScript'],
['Scheme', 'Lua'],
['Self', 'Lua'],
['Self', 'JavaScript']];
function nexts(graph, node) {
if (_.isEmpty(graph)) return [];
var pair = _.first(graph);
var from = _.first(pair);
var to = second(pair);
var more = _.rest(graph);
if (_.isEqual(node, from))
return construct(to, nexts(more, node));
else
return nexts(more, node);
}
function depthSearch(graph, nodes, seen) {
if (_.isEmpty(nodes)) return rev(seen);
var node = _.first(nodes);
var more = _.rest(nodes);
if (_.contains(seen, node))
return depthSearch(graph, more, seen);
else
return depthSearch(graph,
cat(nexts(graph, node), more),
construct(node, seen));
}
function tcLength(ary, n) {
var l = n ? n : 0;
if (_.isEmpty(ary))
return l;
else
return tcLength(_.rest(ary), l + 1);
}
tcLength(_.range(10));
//=> 10
function andify(/* preds */) {
var preds = _.toArray(arguments);
return function(/* args */) {
var args = _.toArray(arguments);
var everything = function(ps, truth) {
if (_.isEmpty(ps))
return truth;
else
return _.every(args, _.first(ps))
&& everything(_.rest(ps), truth);
};
return everything(preds, true);
};
}
function orify(/* preds */) {
var preds = _.toArray(arguments);
return function(/* args */) {
var args = _.toArray(arguments);
var something = function(ps, truth) {
if (_.isEmpty(ps))
return truth;
else
return _.some(args, _.first(ps))
|| something(_.rest(ps), truth);
};
return something(preds, false);
};
}
function evenSteven(n) {
if (n === 0)
return true;
else
return oddJohn(Math.abs(n) - 1);
}
function oddJohn(n) {
if (n === 0)
return false;
else
return evenSteven(Math.abs(n) - 1);
}
function flat(ary) {
if (_.isArray(ary))
return cat.apply(cat, _.map(ary, flat));
else
return [ary];
}
function deepClone(obj) {
if (!existy(obj) || !_.isObject(obj))
return obj;
var temp = new obj.constructor();
for (var key in obj)
if (obj.hasOwnProperty(key))
temp[key] = deepClone(obj[key]);
return temp;
}
function visit(mapFun, resultFun, ary) {
if (_.isArray(ary))
return resultFun(_.map(ary, mapFun));
else
return resultFun(ary);
}
function postDepth(fun, ary) {
return visit(partial1(postDepth, fun), fun, ary);
}
function preDepth(fun, ary) {
return visit(partial1(preDepth, fun), fun, fun(ary));
}
function influencedWithStrategy(strategy, lang, graph) {
var results = [];
strategy(function(x) {
if (_.isArray(x) && _.first(x) === lang)
results.push(second(x));
return x;
}, graph);
return results;
}
function evenOline(n) {
if (n === 0)
return true;
else
return partial1(oddOline, Math.abs(n) - 1);
}
function oddOline(n) {
if (n === 0)
return false;
else
return partial1(evenOline, Math.abs(n) - 1);
}
function trampoline(fun /*, args */) {
var result = fun.apply(fun, _.rest(arguments));
while (_.isFunction(result)) {
result = result();
}
return result;
}
function isEvenSafe(n) {
if (n === 0)
return true;
else
return trampoline(partial1(oddOline, Math.abs(n) - 1));
}
function isOddSafe(n) {
if (n === 0)
return false;
else
return trampoline(partial1(evenOline, Math.abs(n) - 1));
}
function generator(seed, current, step) {
return {
head: current(seed),
tail: function() {
console.log("forced");
return generator(step(seed), current, step);
}
};
}
function genHead(gen) { return gen.head }
function genTail(gen) { return gen.tail() }
var ints = generator(0, _.identity, function(n) { return n+1 });
function genTake(n, gen) {
var doTake = function(x, g, ret) {
if (x === 0)
return ret;
else
return partial(doTake, x-1, genTail(g), cat(ret, genHead(g)));
};
return trampoline(doTake, n, gen, []);
}
function asyncGetAny(interval, urls, onsuccess, onfailure) {
var n = urls.length;
var looper = function(i) {
setTimeout(function() {
if (i >= n) {
onfailure("failed");
return;
}
$.get(urls[i], onsuccess)
.always(function() { console.log("try: " + urls[i]) })
.fail(function() {
looper(i + 1);
});
}, interval);
}
looper(0);
return "go";
}
var groupFrom = curry2(_.groupBy)(_.first);
var groupTo = curry2(_.groupBy)(second);
function influenced(graph, node) {
return _.map(groupFrom(graph)[node], second);
}