-
Notifications
You must be signed in to change notification settings - Fork 101
/
chapter07.js
162 lines (117 loc) · 2.64 KB
/
chapter07.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
var rand = partial1(_.random, 1);
function randString(len) {
var ascii = repeatedly(len, partial1(rand, 26));
return _.map(ascii, function(n) {
return n.toString(36);
}).join('');
}
PI = 3.14;
function areaOfACircle(radius) {
return PI * sqr(radius);
}
areaOfACircle(3);
//=> 28.26
function generateRandomCharacter() {
return rand(26).toString(36);
}
function generateString(charGen, len) {
return repeatedly(len, charGen).join('');
}
var composedRandomString = partial1(generateString, generateRandomCharacter);
composedRandomString(10);
//=> "j18obij1jc"
var a = [1, [10, 20, 30], 3];
var secondTwice = _.compose(second, second);
second(a) === secondTwice(a);
//=> false
function skipTake(n, coll) {
var ret = [];
var sz = _.size(coll);
for(var index = 0; index < sz; index += n) {
ret.push(coll[index]);
}
return ret;
}
function summ(ary) {
var result = 0;
var sz = ary.length;
for (var i = 0; i < sz; i++)
result += ary[i];
return result;
}
summ(_.range(1,11));
//=> 55
function summRec(ary, seed) {
if (_.isEmpty(ary))
return seed;
else
return summRec(_.rest(ary), _.first(ary) + seed);
}
summRec([], 0);
//=> 0
summRec(_.range(1,11), 0);
//=> 55
function deepFreeze(obj) {
if (!Object.isFrozen(obj))
Object.freeze(obj);
for (var key in obj) {
if (!obj.hasOwnProperty(key) || !_.isObject(obj[key]))
continue;
deepFreeze(obj[key]);
}
}
var freq = curry2(_.countBy)(_.identity);
function merge(/*args*/) {
return _.extend.apply(null, construct({}, arguments));
}
function Point(x, y) {
this._x = x;
this._y = y;
}
Point.prototype = {
withX: function(val) {
return new Point(val, this._y);
},
withY: function(val) {
return new Point(this._x, val);
}
};
function Queue(elems) {
this._q = elems;
}
Queue.prototype = {
enqueue: function(thing) {
return new Queue(cat(this._q, [thing]));
}
};
var SaferQueue = function(elems) {
this._q = _.clone(elems);
}
SaferQueue.prototype = {
enqueue: function(thing) {
return new SaferQueue(cat(this._q, [thing]));
}
};
function queue() {
return new SaferQueue(_.toArray(arguments));
}
var q = queue(1,2,3);
var enqueue = invoker('enqueue', SaferQueue.prototype.enqueue);
enqueue(q, 42);
//=> {_q: [1, 2, 3, 42]}
function Container(init) {
this._value = init;
};
Container.prototype = {
update: function(fun /*, args */) {
var args = _.rest(arguments);
var oldValue = this._value;
this._value = fun.apply(this, construct(oldValue, args));
return this._value;
}
};
var aNumber = new Container(42);
aNumber.update(function(n) { return n + 1 });
//=> 43
aNumber;
//=> {_value: 43}