This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ix.js
1249 lines (1147 loc) · 48.8 KB
/
ix.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
(function (root, factory) {
var freeExports = typeof exports == 'object' && exports &&
(typeof root == 'object' && root && root == root.global && (window = root), exports);
// Because of build optimizers
if (typeof define === 'function' && define.amd) {
define(['Ix', 'exports'], function (Ix, exports) {
root.Ix = factory(root, exports, Ix);
return root.Ix;
});
} else if (typeof module == 'object' && module && module.exports == freeExports) {
module.exports = factory(root, module.exports, require('./l2o'));
} else {
root.Ix = factory(root, {}, root.Ix);
}
}(this, function (global, exp, root, undefined) {
var isEqual = root.Internals.isEqual;
function noop () { }
function identity (x) { return x; }
function defaultComparer (x, y) { return x > y ? 1 : x < y ? -1 : 0; }
function defaultEqualityComparer (x, y) { return isEqual(x, y); }
function arrayIndexOf(key, comparer) {
comparer || (comparer = defaultEqualityComparer);
for (var i = 0, len = this.length; i < len; i++) {
if (comparer(key, this[i])) {
return i;
}
}
return -1;
}
var seqNoElements = 'Sequence contains no elements.';
var objectDisposed = 'Object disposed';
var slice = Array.prototype.slice;
var Enumerable = root.Enumerable,
EnumerablePrototype = Enumerable.prototype,
enumerableConcat = Enumerable.concat,
enumerableEmpty = Enumerable.empty,
enumerableFromArray = Enumerable.fromArray,
enumerableRepeat = Enumerable.repeat,
enumeratorCreate = root.Enumerator.create
inherits = root.Internals.inherits;
/**
* Determines whether an enumerable sequence is empty.
* @return {Boolean} true if the sequence is empty; false otherwise.
*/
EnumerablePrototype.isEmpty = function () {
return !this.any();
};
function extremaBy (source, keySelector, comparer) {
var result = [], e = source.getEnumerator();
try {
if (!e.moveNext()) { throw new Error(seqNoElements); }
var current = e.getCurrent(),
resKey = keySelector(current);
result.push(current);
while (e.moveNext()) {
var cur = e.getCurrent(),
key = keySelector(cur),
cmp = comparer(key, resKey);
if (cmp === 0) {
result.push(cur);
} else if (cmp > 0) {
result = [cur];
resKey = key;
}
}
} finally {
e.dispose();
}
return enumerableFromArray(result);
}
/**
* Returns the elements with the minimum key value by using the specified comparer to compare key values.
* @param keySelector Key selector used to extract the key for each element in the sequence.
* @param comparer Comparer used to determine the minimum key value.
* @return List with the elements that share the same minimum key value.
*/
EnumerablePrototype.minBy = function (keySelector, comparer) {
comparer || (comparer = defaultComparer);
return extremaBy(this, keySelector, function (key, minValue) {
return -comparer(key, minValue);
});
};
/**
* Returns the elements with the minimum key value by using the specified comparer to compare key values.
* @param keySelector Key selector used to extract the key for each element in the sequence.
* @param comparer Comparer used to determine the maximum key value.
* @return List with the elements that share the same maximum key value.
*/
EnumerablePrototype.maxBy = function (keySelector, comparer) {
comparer || (comparer = defaultComparer);
return extremaBy(this, keySelector, comparer);
};
var SharedBuffer = (function () {
inherits(SharedBuffer, Enumerable);
function SharedBuffer (source) {
this.disposed = false;
this.source = source;
}
SharedBuffer.prototype.getEnumerator = function () {
if (this.disposed) {
throw new Error('Object disposed');
}
var current, self = this;
return enumeratorCreate(
function () {
if (self.source.moveNext()) {
current = self.source.getCurrent();
return true;
}
return false;
},
function () { return current; });
};
SharedBuffer.prototype.dispose = function () {
if (!this.disposed) {
this.disposed = true;
this.source.dispose();
this.source = null;
}
};
return SharedBuffer;
}());
/**
* Shares the source sequence within a selector function where each enumerator can fetch the next element from the source sequence.
*
* var rng = Enumerable.range(0, 10).share();
*
* var e1 = rng.getEnumerator(); // Both e1 and e2 will consume elements from
* var e2 = rng.getEnumerator(); // the source sequence.
*
* ok(e1.moveNext());
* equal(0, e1.getCurrent());
*
* ok(e1.moveNext());
* equal(1, e1.getCurrent());
*
* ok(e2.moveNext()); // e2 "steals" element 2
* equal(2, e2.getCurrent());
*
* ok(e1.moveNext()); // e1 can't see element 2
* equal(3, e1.getCurrent());
*
* @param {Function} [selector] Selector function with shared access to the source sequence for each enumerator.
* @return Sequence resulting from applying the selector function to the shared view over the source sequence.
*/
EnumerablePrototype.share = function (selector) {
var source = this;
return !selector ?
new SharedBuffer(source.getEnumerator()) :
new Enumerable(function () { return selector(source.share()).getEnumerator(); });
};
function RefCountList(readerCount) {
this.readerCount = readerCount;
this.list = {};
this.length = 0;
}
var RefCountListPrototype = RefCountList.prototype;
RefCountListPrototype.clear = function () {
this.list = {};
this.length = 0;
};
RefCountListPrototype.get = function (i) {
if (!this.list[i]) {
throw new Error('Element no longer available in the buffer.');
}
var res = this.list[i];
if (--res.length === 0) { delete this.list[i]; }
return res.value;
};
RefCountListPrototype.push = function (item) {
this.list[this.length] = { value: item, length: this.readerCount };
this.length++;
};
RefCountListPrototype.done = function (index) {
for (var i = index; i < this.length; i++) {
this.get(i);
}
this.readerCount--;
};
var PublishedBuffer = (function () {
inherits(PublishedBuffer, Enumerable);
function PublishedBuffer(source) {
this.source = source;
this.buffer = new RefCountList(0);
this.disposed = false;
this.stopped = false;
this.error = null;
}
function getEnumerator(i) {
var currentValue, self = this, isDisposed = false, isFirst = true, isDone = false;
return enumeratorCreate(
function () {
if (self.disposed) { throw new Error('Object disposed'); }
if (!isFirst) { i++; }
var hasValue = false, current;
if (i >= self.buffer.length) {
if (!self.stopped) {
try {
hasValue = self.source.moveNext();
if (hasValue) { current = self.source.getCurrent(); }
} catch (e) {
self.stopped = true;
self.error = e;
self.source.dispose();
isDisposed = true;
}
}
if (self.stopped) {
if (self.error) {
self.buffer && self.buffer.done(i + 1);
isDone = true;
throw self.error;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
}
if (hasValue) {
self.buffer.push(current);
}
} else {
hasValue = true;
}
if (hasValue) {
currentValue = self.buffer.get(i);
isFirst = false;
return true;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
},
function () { return currentValue; },
function () { isDone && self.buffer && self.buffer.done(i); }
);
}
PublishedBuffer.prototype.getEnumerator = function () {
if (this.disposed) {
throw new Error('Object disposed');
}
var i = this.buffer.length;
this.buffer.readerCount++;
return getEnumerator.call(this, i);
};
PublishedBuffer.prototype.dispose = function () {
if (!this.disposed) {
this.source.dispose();
this.source = null;
this.buffer.clear();
this.buffer = null;
this.disposed = true;
}
};
return PublishedBuffer;
}());
/**
* Publishes the source sequence within a selector function where each enumerator can obtain a view over a tail of the source sequence.
*
* var rng = Enumerable.Range(0, 10).Publish();
*
* var e1 = rng.getEnumerator(); // e1 has a view on the source starting from element 0
*
* ok(e1.moveNext());
* equal(0, e1.getCurrent());
*
* ok(e1.moveNext());
* equal(1, e1.getCurrent());
*
* var e2 = rng.getEnumerator();
*
* ok(e2.moveNext()); // e2 has a view on the source starting from element 2
* equal(2, e2.getCurrent());
*
* ok(e1.moveNext()); // e1 continues to enumerate over its view
* equal(2, e1.getCurrent());
*
* @param selector Selector function with published access to the source sequence for each enumerator.
* @return Sequence resulting from applying the selector function to the published view over the source sequence.
*/
EnumerablePrototype.publish = function (selector) {
var source = this;
return !selector ?
new PublishedBuffer(source.getEnumerator()) :
new Enumerable(function () { return selector(source.publish()).getEnumerator(); });
};
function MaxRefCountList() {
this.list = [];
this.length = 0;
}
var MaxRefCountListPrototype = MaxRefCountList.prototype;
MaxRefCountListPrototype.done = noop;
MaxRefCountListPrototype.push = function (item) {
this.list[this.length++] = item;
};
MaxRefCountListPrototype.clear = function () {
this.list = [];
this.length = 0;
};
MaxRefCountListPrototype.get = function (i) {
return this.list[i];
};
var MemoizedBuffer = (function () {
inherits(MemoizedBuffer, Enumerable);
function MemoizedBuffer(source, buffer) {
this.source = source;
this.buffer = buffer
this.stopped = false;
this.error = null;
this.disposed = false;
}
MemoizedBuffer.prototype.getEnumerator = function () {
if (this.disposed) {
throw new Error('Object disposed');
}
var i = 0, currentValue, self = this, isDisposed = false, isFirst = true, isDone = false;
return enumeratorCreate(
function () {
if (self.disposed) { throw new Error('Object disposed'); }
if (!isFirst) { i++; }
var hasValue = false, current;
if (i >= self.buffer.length) {
if (!self.stopped) {
try {
hasValue = self.source.moveNext();
if (hasValue) { current = self.source.getCurrent(); }
} catch (e) {
self.stopped = true;
self.error = e;
self.source.dispose();
isDisposed = true;
}
}
if (self.stopped) {
if (self.error) {
self.buffer && self.buffer.done(i + 1);
isDone = true;
throw self.error;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
}
if (hasValue) {
self.buffer.push(current);
}
} else {
hasValue = true;
}
if (hasValue) {
currentValue = self.buffer.get(i);
isFirst = false;
return true;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
},
function () { return currentValue; },
function () { isDone && self.buffer && self.buffer.done(i); }
);
};
MemoizedBuffer.prototype.dispose = function () {
if (!this.disposed) {
this.source.dispose();
this.source = null;
this.buffer.clear();
this.buffer = null;
this.disposed = true;
}
};
return MemoizedBuffer;
}());
/**
* Memoizes the source sequence within a selector function where a specified number of enumerators can get access to all of the sequence's elements without causing multiple enumerations over the source.
*
* var rng = Enumerable.range(0, 10).doAction(function (x) { console.log(x); }).memoize();
*
* var e1 = rng.getEnumerator();
*
* ok(e1.moveNext()); // Prints 0
* equal(0, e1.getCurrent());
*
* ok(e1.moveNext()); // Prints 1
* equal(1, e1.getCurrent());
*
* var e2 = rng.getEnumerator();
*
* ok(e2.moveNext()); // Doesn't print anything; the side-effect of Do
* equal(0, e2.getCurrent()); // has already taken place during e1's iteration.
*
* ok(e1.moveNext()); // Prints 2
* equal(2, e1.getCurrent());
*
* @param readerCount Number of enumerators that can access the underlying buffer. Once every enumerator has obtained an element from the buffer, the element is removed from the buffer.
* @param selector Selector function with memoized access to the source sequence for a specified number of enumerators.
* @return Sequence resulting from applying the selector function to the memoized view over the source sequence.
*/
EnumerablePrototype.memoize = function () {
var source = this;
if (arguments.length === 0) {
return new MemoizedBuffer(source.getEnumerator(), new MaxRefCountList());
} else if (arguments.length === 1 && typeof arguments[0] === 'function') {
return new Enumerable(function () { return arguments[1](source.memoize()).getEnumerator(); });
} else if (arguments.length === 1 && typeof arguments[0] === 'number') {
return new MemoizedBuffer(source.getEnumerator(), new RefCountList(arguments[0]));
} else {
return new Enumerable(function () { return arguments[1](source.memoize(arguments[0])).getEnumerator(); });
}
};
/**
* Returns a sequence that throws an exception upon enumeration. An alias for this method is throwException for <IE9.
* @example
* var result = Enumerable.throw(new Error('error'));
* @param {Object} exception Exception to throw upon enumerating the resulting sequence.
* @returns {Enumerable} Sequence that throws the specified exception upon enumeration.
*/
Enumerable['throw'] = Enumerable.throwException = function (value) {
return new Enumerable(function () {
return enumeratorCreate(
function () { throw value; },
noop);
});
};
/**
* Creates an enumerable sequence based on an enumerable factory function.
* @example
* var result = Enumerable.defer(function () { return Enumerable.range(0, 10); });
* @param {Function} enumerableFactory Enumerable factory function.
* @returns {Enumerable} Sequence that will invoke the enumerable factory upon a call to GetEnumerator.
*/
var enumerableDefer = Enumerable.defer = function (enumerableFactory) {
return new Enumerable(function () {
var enumerator;
return enumeratorCreate(function () {
enumerator || (enumerator = enumerableFactory().getEnumerator());
return enumerator.moveNext();
}, function () {
return enumerator.getCurrent();
}, function () {
enumerator.dispose();
});
});
};
/**
* Generates a sequence by mimicking a for loop.
* @example
* var result = Enumerable.generate(
* 0,
* function (x) { return x < 10; },
* function (x) { return x + 1; },
* function (x) { return x * x });
* @param {Any} initialState Initial state of the generator loop.
* @param {Function} condition Loop condition.
* @param {Function} iterate State update function to run after every iteration of the generator loop.
* @param {Function} resultSelector Result selector to compute resulting sequence elements.
* @returns {Enumerable} Sequence obtained by running the generator loop, yielding computed elements.
*/
Enumerable.generate = function (initialState, condition, iterate, resultSelector) {
return new Enumerable(function () {
var state, current, initialized = false;
return enumeratorCreate(function () {
if (!initialized) {
state = initialState;
initialized = true;
} else {
state = iterate(state);
if (!condition(state)) {
return false;
}
}
current = resultSelector(state);
return true;
}, function () { return current; });
});
};
/**
* Generates a sequence that's dependent on a resource object whose lifetime is determined by the sequence usage duration.
* @example
* var result = Enumerable.using(function () { return new QuerySource(); }, function (x) { return x.get(42); });
* @param {Function} resourceFactory Resource factory function.
* @param {Function} enumerableFactory Enumerable factory function, having access to the obtained resource.
* @returns {Enumerable} Sequence whose use controls the lifetime of the associated obtained resource.
*/
Enumerable.using = function (resourceFactory, enumerableFactory) {
return new Enumerable(function () {
var current, first = true, e, res;
return enumeratorCreate(function () {
if (first) {
res = resourceFactory();
e = enumerableFactory(res).getEnumerator();
first = false;
}
if (!e.moveNext()) {
return false;
}
current = e.getCurrent();
return true;
}, function () {
return current;
}, function () {
e && e.dispose();
res && res.dispose();
});
});
};
function functionBind(f, context) {
return function () {
f.apply(context, arguments);
};
}
/**
* Lazily invokes an action for each value in the sequence, and executes an action upon successful or exceptional termination.
* There is an alias for this method doAction for browsers <IE9.
* @example
* e.do(onNext);
* e.do(onNext, onError);
* e.do(onNExt, onError, onCompleted);
* e.do(observer);
* @param onNext Action to invoke for each element or Observer.
* @param onError Action to invoke on exceptional termination of the sequence.
* @param onCompleted Action to invoke on successful termination of the sequence.
* @returns {Enumerable} Sequence exhibiting the specified side-effects upon enumeration.
*/
EnumerablePrototype['do'] = EnumerablePrototype.doAction = function (onNext, onError, onCompleted) {
var oN, oE, oC, self = this;
if (typeof onNext === 'object') {
oN = functionBind(onNext.onNext, onNext);
oE = functionBind(onNext.onError, onNext);
oC = functionBind(onNext.onCompleted, onNext);
} else {
oN = onNext;
oE = onError || noop;
oC = onCompleted || noop;
}
return new Enumerable(function () {
var e, done, current;
return enumeratorCreate(
function () {
e || (e = self.getEnumerator());
try {
if (!e.moveNext()) {
oC();
return false;
}
current = e.getCurrent();
} catch (e) {
oE(e);
throw e;
}
oN(current);
return true;
},
function () { return current; },
function () { e && e.dispose(); }
);
});
};
/**
* Generates a sequence of buffers over the source sequence, with specified length and possible overlap.
* @example
* var result = Enumerable.range(0, 10).bufferWithCount(2);
* var result = Enumerable.range(0, 10).bufferWithCount(5, 1);
* @param {Number} count Number of elements for allocated buffers.
* @param {Number} [skip] Number of elements to skip between the start of consecutive buffers.
* @returns {Enumerable} Sequence of buffers containing source sequence elements.
*/
EnumerablePrototype.bufferWithCount = function (count, skip) {
var parent = this;
if (skip == null) { skip = count; }
return new Enumerable(function () {
var buffers = [], i = 0, e, current;
return enumeratorCreate(
function () {
e || (e = parent.getEnumerator());
while (true) {
if (e.moveNext()) {
if (i % skip === 0) {
buffers.push([]);
}
for (var idx = 0, len = buffers.length; idx < len; idx++) {
buffers[idx].push(e.getCurrent());
}
if (buffers.length > 0 && buffers[0].length === count) {
current = Enumerable.fromArray(buffers.shift());
++i;
return true;
}
++i;
} else {
if (buffers.length > 0) {
current = Enumerable.fromArray(buffers.shift());
return true;
}
return false;
}
}
},
function () { return current; },
function () { e.dispose(); });
});
};
/**
* Ignores all elements in the source sequence.
* @returns {Enumerable} Source sequence without its elements.
*/
EnumerablePrototype.ignoreElements = function() {
var parent = this;
return new Enumerable(function () {
var e;
return enumeratorCreate(
function () {
e = parent.getEnumerator();
while (e.moveNext()) { }
return false;
},
function () {
throw new Error('Operation is not valid due to the current state of the object.');
},
function () { e.dispose(); }
);
});
};
/**
* Returns elements with a distinct key value by using the specified equality comparer to compare key values.
* @param keySelector Key selector.
* @param comparer Comparer used to compare key values.
* @returns {Enumerable} Sequence that contains the elements from the source sequence with distinct key values.
*/
EnumerablePrototype.distinctBy = function(keySelector, comparer) {
comparer || (comparer = defaultEqualityComparer);
var parent = this;
return new Enumerable(function () {
var current, map = [], e;
return enumeratorCreate(
function () {
e || (e = parent.getEnumerator());
while (true) {
if (!e.moveNext()) { return false; }
var item = e.getCurrent(), key = keySelector(item);
if (arrayIndexOf.call(map, key, comparer) === -1) {
map.push(item);
current = item;
return true;
}
}
},
function () { return current; },
function () { e && e.dispose(); }
);
});
};
/**
* Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.
* @param keySelector Key selector.
* @param comparer Comparer used to compare key values.
* @returns {Enumerable} Sequence without adjacent non-distinct elements.
*/
EnumerablePrototype.distinctUntilChanged = function (keySelector, comparer) {
keySelector || (keySelector = identity);
comparer || (comparer = defaultEqualityComparer);
var parent = this;
return new Enumerable(function () {
var current, e, currentKey, hasCurrentKey;
return enumeratorCreate(
function () {
e || (e = parent.getEnumerator());
while (true) {
if (!e.moveNext()) {
return false;
}
var item = e.getCurrent(),
key = keySelector(item),
comparerEquals = false;
if (hasCurrentKey) {
comparerEquals = comparer(currentKey, key);
}
if (!hasCurrentKey || !comparerEquals) {
current = item;
currentKey = key;
hasCurrentKey = true;
return true;
}
}
},
function () { return current; },
function () { e && e.dispose(); });
});
};
/**
* Expands the sequence by recursively applying a selector function.
* @param selector Selector function to retrieve the next sequence to expand.
* @returns {Enumerable} Sequence with results from the recursive expansion of the source sequence.
*/
EnumerablePrototype.expand = function(selector) {
var parent = this;
return new Enumerable(function () {
var current, q = [parent], inner;
return enumeratorCreate(
function () {
while (true) {
if (!inner) {
if (q.length === 0) { return false; }
inner = q.shift().getEnumerator();
}
if (inner.moveNext()) {
current = inner.getCurrent();
q.push(selector(current));
return true;
} else {
inner.dispose();
inner = null;
}
}
},
function () { return current; },
function () { inner && inner.dispose(); }
);
});
};
/**
* Returns the source sequence prefixed with the specified value.
* @param values Values to prefix the sequence with.
* @returns {Enumerable} Sequence starting with the specified prefix value, followed by the source sequence.
*/
EnumerablePrototype.startWith = function () {
return enumerableConcat(enumerableFromArray(slice.call(arguments)), this);
};
function scan (seed, accumulator) {
var source = this;
return new Enumerable(function () {
var current, e, acc = seed;
return enumeratorCreate(
function () {
e || (e = source.getEnumerator());
if (!e.moveNext()) { return false; }
var item = e.getCurrent();
acc = accumulator(acc, item);
current = acc;
return true;
},
function () { return current; },
function () { e && e.dispose(); }
);
});
}
function scan1 (accumulator) {
var source = this;
return new Enumerable(function () {
var current, e, acc, hasSeed = false;
return enumeratorCreate(
function () {
e || (e = source.getEnumerator());
while(true) {
if (!e.moveNext()) { return false; }
var item = e.getCurrent();
if (!hasSeed) {
hasSeed = true;
acc = item;
continue;
}
acc = accumulator(acc, item);
current = acc;
return true;
}
},
function () { return current; },
function () { e && e.dispose(); }
);
});
}
/**
* Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
* @param seed Accumulator seed value.
* @param accumulator Accumulation function to apply to the current accumulation value and each element of the sequence.
* @returns {Enumerable} Sequence with all intermediate accumulation values resulting from scanning the sequence.
*/
EnumerablePrototype.scan = function (/* seed, accumulator */) {
var f = arguments.length === 1 ? scan1 : scan;
return f.apply(this, arguments);
};
/**
* Returns a specified number of contiguous elements from the end of the sequence.
* @param count The number of elements to take from the end of the sequence.
* @returns {Enumerable} Sequence with the specified number of elements counting from the end of the source sequence.
*/
EnumerablePrototype.takeLast = function (count) {
var parent = this;
return new Enumerable(function () {
var current, e, q;
return enumeratorCreate(
function () {
e || (e = parent.getEnumerator());
if (!q) {
q = [];
while (e.moveNext()) {
q.push(e.getCurrent());
if (q.length > count) {
q.shift();
}
}
}
if (q.length === 0) {
return false;
}
current = q.shift();
return true;
},
function () { return current; },
function () { e && e.dispose(); }
);
});
};
/**
* Bypasses a specified number of contiguous elements from the end of the sequence and returns the remaining elements.
* @param count The number of elements to skip from the end of the sequence before returning the remaining elements.
* @returns {Enumerable} Sequence bypassing the specified number of elements counting from the end of the source sequence.
*/
EnumerablePrototype.skipLast = function (count) {
var parent = this;
return new Enumerable(function () {
var current, e, q = [];
return enumeratorCreate(
function () {
e || (e = parent.getEnumerator());
while (true) {
if (!e.moveNext()) {
return false;
}
q.push(e.getCurrent());
if (q.length > count) {
current = q.shift();
return true;
}
}
},
function () { return current; },
function () { e && e.dispose(); }
);
});
};
/**
* Repeats and concatenates the source sequence the given number of times.
* @param count Number of times to repeat the source sequence.
* @returns {Enumerable} Sequence obtained by concatenating the source sequence to itself the specified number of times.
*/
EnumerablePrototype.repeat = function (count) {
var parent = this;
return enumerableRepeat(0, count).selectMany(function () { return parent; });
};
function catchExceptionHandler (source, handler) {
return new Enumerable(function () {
var current, e, errE;
return enumeratorCreate(
function () {
e || (e = source.getEnumerator());
while (true) {
var b, c;
try {
b = e.moveNext();
c = e.getCurrent();
} catch (e) {
errE = handler(e);
break;
}
if (!b) {
return false;
}
current = c;
return true;
}
if (errE) {
e.dispose();
e = errE.getEnumerator();
if (!e.moveNext()) { return false; }
current = e.getCurrent();
return true;
}
},
function () { return current; },
function () {
e && e.dispose();
});
});
}
/**
* Creates a sequence that returns the elements of the first sequence, switching to the second in case of an error.
* An alias for this method is catchException for browsers <IE9.
* @param second Second sequence, concatenated to the result in case the first sequence completes exceptionally or handler to invoke when an exception of the specified type occurs.
* @returns {Enumerable} The first sequence, followed by the second sequence in case an error is produced.
*/
EnumerablePrototype['catch'] = EnumerablePrototype.catchException = function (secondOrHandler) {
if (arguments.length === 0) {
return enumerableCatch(this); // Already IE<IE<T>>
} else if (typeof secondOrHandler === 'function') {
return catchExceptionHandler(this, secondOrHandler); // use handler
} else {
var args = slice.call(arguments);
args.unshift(this);
return enumerableCatch.apply(null, args); // create IE<IE<T>>
}
};
/**
* Creates a sequence by concatenating source sequences until a source sequence completes successfully.
* An alias for this method is catchException for browsers <IE9.
* @returns {Enumerable} Sequence that continues to concatenate source sequences while errors occur.
*/
var enumerableCatch = Enumerable['catch'] = Enumerable.catchException = function () {
// Check arguments
var sources = Enumerable.fromArray(arguments);