-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.3.js
executable file
·209 lines (175 loc) · 3.66 KB
/
7.3.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
function curry(fn, args, length) {
return function (arg) {
var allArgs = (args || []).concat([].slice.call(arguments, 0, 1));
length = length || fn.length;
if (allArgs.length < length) {
return curry(fn, allArgs);
}
return fn.apply(this, allArgs);
};
}
function autoCurry(fn, length) {
length = length || fn.length;
return function () {
if (arguments.length >= length) {
return fn.apply(this, arguments);
} else {
return curry(fn, [].slice.call(arguments, 0));
}
};
}
function partial(fn) {
var args = [].slice.call(arguments, 1);
return function () {
return fn.apply(this, args.concat([].slice.call(arguments)));
};
};
var _nth = autoCurry(function (n, list) {
return list[n];
});
var first = partial(_nth, 0);
var second = partial(_nth, 1);
var eq = autoCurry(function (val, other) {
return val === other;
});
function compose() {
var fns = [].slice.call(arguments).reverse();
return function () {
return first(fns.reduce(function (args, fn) {
return [fn.apply(null, args)];
}, arguments));
};
}
function identity(v) { return v; };
function rest(list) {
return list.slice(1);
}
function makeList(first, rest) {
return [first].concat(rest);
}
var prop = autoCurry(function (prop, obj) {
console.log('prop(' + prop + ', ' + JSON.stringify(obj) + ')');
return obj[prop];
});
function renderBlogPost(post) {
console.log(post.title);
}
function square(n) {
return n * n;
}
function nth(n, sequence) {
if (sequence === null) {
return null;
}
if (n === 0) {
return sequence.first();
} else {
return nth(n - 1, sequence.rest());
}
}
function isEven(num) {
console.log('isEven(' + num + ')');
return num % 2 === 0;
}
function trampoline(fn) {
while (typeof fn === 'function') {
fn = fn();
}
return fn;
}
var func = autoCurry(function (prop, obj) {
return obj ? obj[prop]() : null;
});
function lazy(source, first, rest) {
if (source === null) {
return null;
}
var head, tail;
return {
first: function () {
if (!head) {
head = first(source);
}
return head;
},
rest: function () {
if (!tail) {
tail = partial(lazy, rest(source), first, rest);
}
return trampoline(tail);
}
};
}
function nullifyEmpty(list) {
return list.length === 0 ? null : list;
}
function sequence(list) {
return lazy(list, first, compose(nullifyEmpty, rest));
}
function map(fn, seq) {
return lazy(seq, compose(fn, func('first')), func('rest'));
}
function filter(pred, seq) {
function findFirst(seq) {
while (seq) {
if (pred(seq.first())) {
return seq;
}
seq = seq.rest();
}
}
return lazy(
seq,
compose(func('first'), findFirst),
compose(func('rest'), findFirst)
);
}
function realize(seq) {
var arr = [];
while (seq) {
if (seq.first()) {
arr.push(seq.first());
}
seq = seq.rest();
}
return arr;
}
function isPrime(n) {
if (n === 2) {
return true;
}
if (n < 2) {
return false;
}
for (var i = 2; i <= Math.ceil(Math.sqrt(n)); ++i) {
if (n % i === 0) { return false; }
}
return true;
}
function inc(n) {
return n + 1;
}
function take(n, seq) {
var arr = [];
while (seq && n--) {
arr.push(seq.first());
seq = seq.rest();
}
return arr;
}
function drop(n, seq) {
while (n--) {
seq = seq.rest();
}
return seq;
}
function slice(start, length, seq) {
return take(length, drop(start, seq));
}
function repeat(val) {
return lazy(val, identity, identity);
}
function randomInt(max) {
return Math.floor(Math.random() * max);
}
console.log(take(5, map(compose(inc, randomInt), repeat(6))));