-
Notifications
You must be signed in to change notification settings - Fork 1
/
iter.js
294 lines (261 loc) · 4.72 KB
/
iter.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
+function(exports,undefined){
function iter(arr){
return new Iterator(arr);
}
function* wrap(arr){
for(var i =0;i<arr.length;i++){
yield arr[i];
}
}
//map,where,concat,take
//sort,groupby
//reduce,max,min,sum,avg,stat
function Iterator(arr){
this.gen = wrap(arr);
}
Iterator.prototype = {
where:where,
map:map,
concat:concat,
take:take,
groupBy:groupBy,
sort:sort,
max:max,
min:min,
avg:avg,
sum:sum,
count:count,
stat:stat,
reduce:reduce,
toArray:toArray,
}
function* mapImpl(gen,f){
var _g;
while(true){
_g =gen.next();
if(_g.done) break;
yield f(_g.value);
}
}
/*
* map (T,index) -> U
*
* @param {generator} gen
* @f {Function} f : (T,index)->U
* @api private
*/
function map(f){
var gen = mapImpl(this.gen,f);
this.gen =gen;
return this;
}
function* whereImple(gen,f){
var index = 0, _g,done,value;
while(true){
_g = gen.next();
if(_g.done) break;
value = _g.value;
if(f(value,index++)){
yield value;
}
}
}
//@param {Funciton} f: filter function,(T,index)->boolean
function where(f){
var gen = whereImple(this.gen,f);
this.gen = gen;
return this;
}
function* concatImpl(gen,otherArr,f){
var f = f||defaultConcat;
var index = 0, _g;
while(true){
_g = gen.next();
if(_g.done) break;
yield _g.value;
}
for(var i=0;i<otherArr.length;i++){
yield otherArr[i];
}
}
//@param {Funciton} f: how concat two values,(T,U)->V
function concat(arr,f){
//var f = f||defaultConcat;
var gen = concatImpl(this.gen,arr,f);
this.gen = gen;
return this;
}
//n should be number
function* takeImpl(gen,n){
var _g,index=0;
while(true){
_g = gen.next();
if(_g.done || (index++)>=n ) break;
yield _g.value;
}
}
function take(n){
this.gen = takeImpl(this.gen,n);
return this;
}
function reduce(f,initValue){
var gen = this.gen
var _g;
if(initValue==undefined){
_g=gen.next();
if(_g.done) {
return undefined
}
else{
initValue = _g.value;
}
}
var result=initValue;
while(true){
_g = gen.next();
if(_g.done) break;
result = f(result,_g.value);
}
return result;
}
function* groupByImpl(gen,key){
var getKey = (typeof key =='function')?key:createKey(key);
var group={};
var _g,_k,value;
while(true){
_g = gen.next();
if(_g.done) break;
value = _g.value;
_k = getKey(value);
group[_k]
if(_k in group){
group[_k].push(value)
}else{
group[_k]=[];
group[_k].push(value);
}
}
for(var k in group){
yield {'key':k,'values':group[k]};
}
}
//@param ,{String|Function}key
function groupBy(key){
this.gen = groupByImpl(this.gen,key);
return this;
}
function* sortImpl(gen,f){
f=f||defaultSort;
var arr = toArrayImpl(gen).sort(f);
for(var i=0;i<arr.length;i++){
yield arr[i];
}
}
function sort(f){
this.gen = sortImpl(this.gen,f);
return this;
}
function sum(){
var self =this;
return reduce.call(self,function(v1,v2){
return v1+v2
},0);
}
function min(f){
f=f||defaultMin;
var self =this;
return reduce.call(self,f);
}
function max(f){
f=f||defaultMax;
var self =this;
return reduce.call(self,f);
}
function count(){
var gen = this.gen;
var count=0;
var _g;
while(true){
_g = gen.next();
if(_g.done) break;
count++;
}
return count;
}
//just for numbers;
function avg(){
var gen = this.gen;
var sum=0,count=0,_g;
while(true){
_g = gen.next();
if(_g.done) break;
sum+=_g.value;
count++;
}
return sum/count;
}
//just for numbers
function stat(){
var gen = this.gen;
var _g,value;
var count=0;
var sum = 0;
var max =Number.MIN_VALUE;
var min = Number.MAX_VALUE;
while(true){
_g = gen.next();
if(_g.done) break;
value = _g.value;
sum+=value;;
count++;
if(max<value)max=value;
if(min>value)min = value;
}
var avg = sum/count;
return {
min:min,
max:max,
sum:sum,
avg:avg,
count:count
};
}
function toArrayImpl(gen){
var arr = [];
var _g;
while (true){
_g=gen.next();
if(_g.done)break;
arr.push(_g.value);
};
return arr;
}
function toArray(){
var gen = this.gen;
return toArrayImpl(gen);
}
//--------------------------------------
//default function for some functionality
//--------------------------------------
//default function for concat
function defaultConcat(va1,va2){
return (va1 + va2);
}
function defaultMin(v1,v2){
return (v1<v2)?v1:v2;
}
function defaultMax(v1,v2){
return (v1>v2)?v1:v2;
}
function defaultSort(v1,v2){
if(v1>v2)return 1;
if(v1<v2)return -1;
return 0;
}
function createKey(key){
return function(value){
return value[key];
}
}
exports.iter = iter;
}(window)