-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.1.js
executable file
·158 lines (133 loc) · 3.16 KB
/
7.1.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
var blogPosts = [
{author: {email: '[email protected]'}, title: 'Thinking outside the DOM'},
{author: {email: '[email protected]'}, title: 'Introducing Stasis'},
{author: {email: '[email protected]'}, title: 'Working with React'},
{author: {email: '[email protected]'}, title: 'Functional programming'}
];
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) {
return obj[prop];
});
function renderBlogPost(post) {
console.log(post.title);
}
function square(n) {
console.log('Squaring', 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 sequence(list) {
return {
first: partial(first, list),
rest: compose(sequence, partial(rest, list))
};
}
function map(fn, seq) {
function lazy(sequence) {
if (sequence === null) {
return null;
}
return {
first: function () {
return fn(sequence.first());
},
rest: function () {
return lazy(sequence.rest());
}
};
}
return lazy(seq);
}
function filter(pred, seq) {
function lazy(sequence) {
return {
first: function () {
var seq = sequence;
while (seq) {
if (pred(seq.first())) {
return seq.first();
}
seq = seq.rest();
}
},
rest: function () {
var seq = sequence;
while (seq) {
if (pred(seq.first())) {
return seq.rest();
}
seq = seq.rest();
}
}
};
}
return lazy(seq);
}
function isEven(num) {
return num % 2 === 0;
}
var list = sequence([{id: 0}, {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}, {id: 7}, {id: 8}, {id: 9}]);
var mapped = map(function (i) {
console.log('Mapping', i);
return i.id;
}, list);
var res = filter(isEven, mapped);
console.log(nth(1, res));
console.log(nth(4, res));