forked from funjs/book-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter01.js
167 lines (126 loc) · 3.11 KB
/
chapter01.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
function splat(fun) {
return function(array) {
return fun.apply(null, array);
};
}
var addArrayElements = splat(function(x, y) { return x + y });
addArrayElements([1, 2]);
//=> 3
function unsplat(fun) {
return function() {
return fun.call(null, _.toArray(arguments));
};
}
var joinElements = unsplat(function(array) { return array.join(' ') });
joinElements(1, 2);
//=> "1 2"
joinElements('-', '$', '/', '!', ':');
//=> "- $ / ! :"
function parseAge(age) {
if (!_.isString(age)) throw new Error("Expecting a string");
var a;
console.log("Attempting to parse an age");
a = parseInt(age, 10);
if (_.isNaN(a)) {
console.log(["Could not parse age:", age].join(' '));
a = 0;
}
return a;
}
function fail(thing) {
throw new Error(thing);
}
function warn(thing) {
console.log(["WARNING:", thing].join(' '));
}
function note(thing) {
console.log(["NOTE:", thing].join(' '));
}
function parseAge(age) {
if (!_.isString(age)) fail("Expecting a string");
var a;
note("Attempting to parse an age");
a = parseInt(age, 10);
if (_.isNaN(a)) {
warn(["Could not parse age:", age].join(' '));
a = 0;
}
return a;
}
var letters = ['a', 'b', 'c'];
letters[1];
//=> 'b'
function naiveNth(a, index) {
return a[index];
}
function isIndexed(data) {
return _.isArray(data) || _.isString(data);
}
function nth(a, index) {
if (!_.isNumber(index)) fail("Expected a number as the index");
if (!isIndexed(a)) fail("Not supported on non-indexed type");
if ((index < 0) || (index > a.length - 1))
fail("Index value is out of bounds");
return a[index];
}
function second(a) {
return nth(a, 1);
}
function compareLessThanOrEqual(x, y) {
if (x < y) return -1;
if (y < x) return 1;
return 0;
}
[2, 3, -1, -6, 0, -108, 42, 10].sort(compareLessThanOrEqual);
//=> [-108, -6, -1, 0, 2, 3, 10, 42]
function lessOrEqual(x, y) {
return x <= y;
}
function comparator(pred) {
return function(x, y) {
if (truthy(pred(x, y)))
return -1;
else if (truthy(pred(y, x)))
return 1;
else
return 0;
};
};
function lameCSV(str) {
return _.reduce(str.split("\n"), function(table, row) {
table.push(_.map(row.split(","), function(c) { return c.trim()}));
return table;
}, []);
};
var peopleTable = lameCSV("name,age,hair\nMerble,35,red\nBob,64,blonde");
peopleTable;
//=> [["name", "age", "hair"],
// ["Merble", "35", "red"],
// ["Bob", "64", "blonde"]]
function selectNames(table) {
return _.rest(_.map(table, _.first));
}
function selectAges(table) {
return _.rest(_.map(table, second));
}
function selectHairColor(table) {
return _.rest(_.map(table, function(row) {
return nth(row, 2);
}));
}
var mergeResults = _.zip;
function existy(x) { return x != null };
function truthy(x) { return (x !== false) && existy(x) };
function doWhen(cond, action) {
if(truthy(cond))
return action();
else
return undefined;
}
function executeIfHasField(target, name) {
return doWhen(existy(target[name]), function() {
var result = _.result(target, name);
console.log(['The result is', result].join(' '));
return result;
});
}