/
Vector.ts
986 lines (903 loc) · 30.5 KB
/
Vector.ts
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
import { inspect } from './Value';
import { Option } from "./Option";
import { HashMap } from "./HashMap";
import { HashSet } from "./HashSet";
import { Stream } from "./Stream";
import { Seq, IterableArray } from "./Seq";
import { WithEquality, areEqual, getHashCode,
Ordering, ToOrderable } from "./Comparison";
import * as SeqHelpers from "./SeqHelpers";
import * as L from "list";
/**
* A general-purpose list class with all-around good performance.
* quasi-O(1) (actually O(log32(n))) access, append, replace.
* It's backed by a bit-mapped vector trie.
* @param T the item type
*/
export class Vector<T> implements Seq<T> {
/**
* @hidden
*/
// _contents will be undefined only if length===0
protected constructor(private _list: L.List<T>) {}
/**
* The empty vector.
* @param T the item type
*/
static empty<T>(): Vector<T> {
return new Vector(L.empty());
}
/**
* Build a vector from a series of items (any number, as parameters)
* @param T the item type
*/
static of<T>(...data: T[]): Vector<T> {
return Vector.ofIterable(data);
}
/**
* Build a vector from any iterable, which means also
* an array for instance.
* @param T the item type
*/
static ofIterable<T>(elts: Iterable<T>): Vector<T> {
return new Vector(L.from(elts));
}
/**
* Curried predicate to find out whether the vector is empty.
*
* LinkedList.of(Vector.of(1), Vector.empty<number>())
* .filter(Vector.isEmpty)
* => LinkedList.of(Vector.empty<number>())
*/
static isEmpty<T>(v: Vector<T>): boolean {
return v.isEmpty();
}
/**
* Curried predicate to find out whether the vector is empty.
*
* LinkedList.of(Vector.of(1), Vector.empty<number>())
* .filter(Vector.isNotEmpty)
* => LinkedList.of(Vector.of(1))
*/
static isNotEmpty<T>(v: Vector<T>): boolean {
return !v.isEmpty();
}
/**
* Get the length of the collection.
*/
length(): number {
return L.length(this._list);
}
/**
* true if the collection is empty, false otherwise.
*/
isEmpty(): boolean {
return L.length(this._list) === 0;
}
/**
* Dual to the foldRight function. Build a collection from a seed.
* Takes a starting element and a function.
* It applies the function on the starting element; if the
* function returns None, it stops building the list, if it
* returns Some of a pair, it adds the first element to the result
* and takes the second element as a seed to keep going.
*
* Vector.unfoldRight(
* 10, x=>Option.of(x)
* .filter(x => x!==0)
* .map<[number,number]>(x => [x,x-1]))
* => Vector.of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
*/
static unfoldRight<T,U>(seed: T, fn: (x:T)=>Option<[U,T]>): Vector<U> {
let nextVal = fn(seed);
let r = L.empty();
while (nextVal.isSome()) {
r = L.append(nextVal.get()[0], r);
nextVal = fn(nextVal.get()[1]);
}
return new Vector(r);
}
/**
* Retrieve the element at index idx.
* Returns an option because the collection may
* contain less elements than the index.
*/
get(index: number): Option<T> {
return Option.of(L.nth(index, this._list));
}
/**
* If the collection contains a single element,
* return Some of its value, otherwise return None.
*/
single(): Option<T> {
return L.length(this._list) === 1 ?
this.head() :
Option.none<T>();
}
/**
* Replace the value of element at the index you give.
* Will throw if the index is out of bounds!
*/
replace(index: number, val: T): Vector<T> {
if (index >= this.length() || index < 0) {
throw new Error('Vector.replace: index is out of range: ' + index);
}
return new Vector(L.update(index, val, this._list));
}
/**
* Replace the first occurence (if any) of the element you give by
* the new value you give.
*
* Vector.of(1, 2, 3, 4, 2).replaceFirst(2, 5)
* => Vector.of(1, 5, 3, 4, 2)
*
*/
replaceFirst(element: T&WithEquality, newVal: T&WithEquality): Vector<T> {
// it's a little annoying that areEqual will check whether the element
// has an equals function for each element in the list, but then
// what if the list allows null or undefined and the newVal is null or
// undefined? With type erasure then I don't know what equality to use
// on the next elements
const index = L.findIndex(v => areEqual(v, element), this._list);
return (index >= 0)
? new Vector(L.update(index, newVal, this._list))
: this;
}
/**
* Replace all occurences of the element you give by
* the new value you give.
*
* Vector.of(1, 2, 3, 4, 2).replaceAll(2, 5)
* => Vector.of(1, 5, 3, 4, 5)
*
*/
replaceAll(element: T&WithEquality, newVal: T&WithEquality): Vector<T> {
// if we're going to update many elements, then append in a loop
// would give better perf (not copying multiple times the same slice).
// if we won't update that many, update in a loop would give better perf...
// assuming it's the latter case.
let idx = 0;
return this.foldLeft(
this as Vector<T>,
(sofar, cur) => {
const r = areEqual(cur, element)
? new Vector(L.update(idx, newVal, sofar._list))
: sofar;
++idx;
return r;
});
}
/**
* Append an element at the end of the collection.
*/
append(val:T): Vector<T> {
return new Vector(L.append(val, this._list));
}
/**
* Append multiple elements at the end of the collection.
* Note that arrays are also iterables.
*/
appendAll(elts: Iterable<T>): Vector<T> {
if ((<any>elts)._list && (<any>elts).replace) {
// elts is a vector too
return new Vector(L.concat(this._list, (<Vector<T>>elts)._list));
}
return new Vector(L.concat(this._list, L.from(elts)));
}
/**
* Remove multiple elements from a vector
*
* Vector.of(1,2,3,4,3,2,1).removeAll([2,4])
* => Vector.of(1,3,3,1)
*/
removeAll(elts:Iterable<T&WithEquality>): Vector<T> {
return <Vector<T>><any>SeqHelpers.removeAll(this, elts);
}
/**
* Get the first value of the collection, if any.
* returns Option.Some if the collection is not empty,
* Option.None if it's empty.
*/
head(): Option<T> {
return this.get(0);
}
/**
* Get the last value of the collection, if any.
* returns Option.Some if the collection is not empty,
* Option.None if it's empty.
*/
last(): Option<T> {
return Option.of(L.last(this._list));
}
/**
* Return a new vector containing all the elements in this
* vector except the last one, or the empty vector if this
* is the empty vector.
*
* Vector.of(1,2,3).init()
* => Vector.of(1,2)
*/
init(): Vector<T> {
return new Vector(L.pop(this._list));
}
/**
* Returns a new collection, discarding the first elements
* until one element fails the predicate. All elements
* after that point are retained.
*/
dropWhile(predicate:(x:T)=>boolean): Vector<T> {
return new Vector(L.dropWhile(predicate, this._list));
}
/**
* Search for the first item matching the predicate you pass,
* return Option.Some of that element if found,
* Option.None otherwise.
*/
find(predicate:(v:T)=>boolean): Option<T> {
return Option.of(L.find(predicate, this._list));
}
/**
* Search for the last item matching the predicate you pass,
* return Option.Some of that element if found,
* Option.None otherwise.
*/
findLast(predicate:(v:T)=>boolean): Option<T> {
return Option.of(L.findLast(predicate, this._list));
}
/**
* Search for the first item matching the predicate you pass,
* returning its index in the form of Option.Some if found,
* Option.None otherwise.
*/
findIndex(predicate:(v:T)=>boolean): Option<number> {
return Option.of(L.findIndex(predicate, this._list)).filter(i => i != -1);
}
/**
* Returns true if the predicate returns true for all the
* elements in the collection.
*/
allMatch(predicate:(v:T)=>boolean): boolean {
return L.every(predicate, this._list);
}
/**
* Returns true if there the predicate returns true for any
* element in the collection.
*/
anyMatch(predicate:(v:T)=>boolean): boolean {
return L.some(predicate, this._list);
}
/**
* Returns a pair of two collections; the first one
* will only contain the items from this collection for
* which the predicate you give returns true, the second
* will only contain the items from this collection where
* the predicate returns false.
*
* Vector.of(1,2,3,4).partition(x => x%2===0)
* => [Vector.of(2,4),Vector.of(1,3)]
*/
partition<U extends T>(predicate:(v:T)=>v is U): [Vector<U>,Vector<Exclude<T,U>>];
partition(predicate:(x:T)=>boolean): [Vector<T>,Vector<T>];
partition(predicate:(v:T)=>boolean): [Vector<T>,Vector<T>] {
return <[Vector<T>,Vector<T>]>L.partition(predicate, this._list)
.map(x => new Vector(x));
}
/**
* Returns true if the item is in the collection,
* false otherwise.
*/
contains(v:T&WithEquality): boolean {
return this.find(x => areEqual(x,v)).isSome();
}
/**
* Group elements in the collection using a classifier function.
* Elements are then organized in a map. The key is the value of
* the classifier, and in value we get the list of elements
* matching that value.
*
* also see [[Vector.arrangeBy]]
*/
groupBy<C>(classifier: (v:T)=>C & WithEquality): HashMap<C,Vector<T>> {
return this.foldLeft(
HashMap.empty<C,Vector<T>>(),
(acc: HashMap<C,Vector<T>>, v:T) =>
acc.putWithMerge(
classifier(v), Vector.of(v), // !!! DOUBLE CHECK THIS
(v1:Vector<T>,v2:Vector<T>) => v1.append(<T>L.nth(0, v2._list))));
}
/**
* Matches each element with a unique key that you extract from it.
* If the same key is present twice, the function will return None.
*
* also see [[Vector.groupBy]]
*/
arrangeBy<K>(getKey: (v:T)=>K&WithEquality): Option<HashMap<K,T>> {
return SeqHelpers.arrangeBy<T,K>(this, getKey);
}
/**
* Remove duplicate items; elements are mapped to keys, those
* get compared.
*
* Vector.of(1,1,2,3,2,3,1).distinctBy(x => x);
* => Vector.of(1,2,3)
*/
distinctBy<U>(keyExtractor: (x:T)=>U&WithEquality): Vector<T> {
return <Vector<T>>SeqHelpers.distinctBy(this, keyExtractor);
}
[Symbol.iterator](): Iterator<T> {
return this._list[Symbol.iterator]();
}
/**
* Call a function for element in the collection.
*/
forEach(fun:(x:T)=>void):Vector<T> {
L.forEach(fun, this._list);
return this;
}
/**
* Return a new collection where each element was transformed
* by the mapper function you give.
*/
map<U>(fun:(x:T)=>U): Vector<U> {
return new Vector(L.map(fun, this._list));
}
/**
* Call a predicate for each element in the collection,
* build a new collection holding only the elements
* for which the predicate returned true.
*/
filter<U extends T>(fun:(v:T)=>v is U): Vector<U>;
filter(fun:(v:T)=>boolean): Vector<T>;
filter(fun:(v:T)=>boolean): Vector<T> {
return new Vector(L.filter(fun, this._list));
}
/**
* Apply the mapper function on every element of this collection.
* The mapper function returns an Option; if the Option is a Some,
* the value it contains is added to the result Collection, if it's
* a None, the value is discarded.
*
* Vector.of(1,2,6).mapOption(x => x%2===0 ?
* Option.of(x+1) : Option.none<number>())
* => Vector.of(3, 7)
*/
mapOption<U>(mapper:(v:T)=>Option<U>): Vector<U> {
let vec = L.empty();
for (let i = 0; i < this.length(); i++) {
const v = mapper(<T>L.nth(i, this._list));
if (v.isSome()) {
vec = L.append(v.get(), vec);
}
}
return new Vector(vec);
}
/**
* Calls the function you give for each item in the collection,
* your function returns a collection, all the collections are
* concatenated.
* This is the monadic bind.
*/
flatMap<U>(mapper:(v:T)=>Vector<U>): Vector<U> {
return new Vector(L.chain(x => mapper(x)._list, this._list));
}
/**
* Reduces the collection to a single value using the
* associative binary function you give. Since the function
* is associative, order of application doesn't matter.
*
* Example:
*
* Vector.of(1,2,3).fold(0, (a,b) => a + b);
* => 6
*/
fold(zero:T, fn:(v1:T,v2:T)=>T): T {
return this.foldLeft(zero, fn);
}
/**
* Reduces the collection to a single value.
* Left-associative.
*
* Example:
*
* Vector.of("a", "b", "c").foldLeft("!", (xs,x) => x+xs);
* => "cba!"
*
* @param zero The initial value
* @param fn A function taking the previous value and
* the current collection item, and returning
* an updated value.
*/
foldLeft<U>(zero:U, fn:(soFar:U,cur:T)=>U):U {
return L.foldl(fn, zero, this._list);
}
/**
* Reduces the collection to a single value.
* Right-associative.
*
* Example:
*
* Vector.of("a", "b", "c").foldRight("!", (x,xs) => xs+x);
* => "!cba"
*
* @param zero The initial value
* @param fn A function taking the current collection item and
* the previous value , and returning
* an updated value.
*/
foldRight<U>(zero: U, fn:(cur:T, soFar:U)=>U): U {
return L.foldr(fn, zero, this._list);
}
/**
* Returns the index of the first occurence of the value you give, if present
*
* Vector.of(1, 2, 3, 4, 3).indexOf(3)
* => Option.of(2)
*/
indexOf(element: T & WithEquality): Option<number> {
return Option.of(L.findIndex(v => areEqual(v, element), this._list))
.filter(i => i >= 0);
}
/**
* Randomly reorder the elements of the collection.
*/
shuffle(): Vector<T> {
return Vector.ofIterable(SeqHelpers.shuffle(this.toArray()));
}
/**
* Transform this value to another value type.
* Enables fluent-style programming by chaining calls.
*/
transform<U>(converter:(x:Vector<T>)=>U): U {
return converter(this);
}
/**
* Two objects are equal if they represent the same value,
* regardless of whether they are the same object physically
* in memory.
*/
equals(other:Vector<T&WithEquality>): boolean {
if (<any>other === this) {
return true;
}
if (!other || (!other._list) || (!L.isList(other._list))) {
return false;
}
if (this.length() !== other.length()) return false;
for (let i = 0; i < this.length(); i++) {
const myVal: T & WithEquality|null|undefined = <T&WithEquality>L.nth(i, this._list);
const hisVal: T & WithEquality|null|undefined = L.nth(i, other._list);
if ((myVal === undefined) !== (hisVal === undefined)) {
return false;
}
if (myVal === undefined || hisVal === undefined) {
// they are both undefined, the || is for TS's flow analysis
// so he realizes none of them is undefined after this.
continue;
}
if (!areEqual(myVal, hisVal)) {
return false;
}
}
return true;
}
/**
* Get a number for that object. Two different values
* may get the same number, but one value must always get
* the same number. The formula can impact performance.
*/
hashCode(): number {
let hash = 1;
for (let i=0;i<this.length();i++) {
hash = 31 * hash + getHashCode(L.nth(i, this._list));
}
return hash;
}
/**
* Get a human-friendly string representation of that value.
*
* Also see [[Vector.mkString]]
*/
toString(): string {
let r = "Vector(";
for (let i=0;i<this.length();i++) {
if (i>0) {
r += ", ";
}
r += SeqHelpers.toStringHelper(L.nth(i, this._list));
}
return r + ")";
}
/**
* Used by the node REPL to display values.
* Most of the time should be the same as toString()
*/
[inspect](): string {
return this.toString();
}
/**
* Joins elements of the collection by a separator.
* Example:
*
* Vector.of(1,2,3).mkString(", ")
* => "1, 2, 3"
*/
mkString(separator: string): string {
let r = "";
for (let i=0;i<this.length();i++) {
if (i>0) {
r += separator;
}
r += SeqHelpers.toStringHelper(<T>L.nth(i, this._list), {quoteStrings:false});
}
return r;
}
/**
* Returns a new collection with elements
* sorted according to the comparator you give.
*
* also see [[Vector.sortOn]]
*/
sortBy(compare: (v1:T,v2:T)=>Ordering): Vector<T> {
return Vector.ofIterable<T>(this.toArray().sort(compare));
}
/**
* Give a function associating a number or a string with
* elements from the collection, and the elements
* are sorted according to that value.
*
* Vector.of({a:3,b:"b"},{a:1,b:"test"},{a:2,b:"a"}).sortOn(elt=>elt.a)
* => Vector.of({a:1,b:"test"},{a:2,b:"a"},{a:3,b:"b"})
*
* You can also sort by multiple criteria, and request 'descending'
* sorting:
*
* Vector.of({a:1,b:"b"},{a:1,b:"test"},{a:2,b:"a"}).sortOn(elt=>elt.a,{desc:elt=>elt.b})
* => Vector.of({a:1,b:"test"},{a:1,b:"b"},{a:2,b:"a"})
*
* also see [[Vector.sortBy]]
*/
sortOn(...getKeys: Array<ToOrderable<T>|{desc:ToOrderable<T>}>): Vector<T> {
return <Vector<T>>SeqHelpers.sortOn<T>(this, getKeys);
}
/**
* Convert this collection to a map. You give a function which
* for each element in the collection returns a pair. The
* key of the pair will be used as a key in the map, the value,
* as a value in the map. If several values get the same key,
* entries will be lost.
*
* Vector.of(1,2,3).toMap(x=>[x.toString(), x])
* => HashMap.of(["1",1], ["2",2], ["3",3])
*/
toMap<K,V>(converter:(x:T)=>[K & WithEquality,V]): HashMap<K,V> {
return this.foldLeft(HashMap.empty<K,V>(), (acc,cur) => {
const converted = converter(cur);
return acc.put(converted[0], converted[1]);
});
}
/**
* Convert this collection to a set. Since the elements of the
* Seq may not support equality, you must pass a function returning
* a value supporting equality.
*
* Vector.of(1,2,3,3,4).toSet(x=>x)
* => HashSet.of(1,2,3,4)
*/
toSet<K>(converter:(x:T)=>K&WithEquality): HashSet<K> {
return this.foldLeft(HashSet.empty<K>(), (acc,cur) => {
return acc.add(converter(cur));
});
}
/**
* Convert to array.
*/
toArray(): T[] {
return L.toArray(this._list);
};
/**
* @hidden
*/
hasTrueEquality(): boolean {
return SeqHelpers.seqHasTrueEquality<T>(this);
}
/**
* Combine any number of iterables you give in as
* parameters to produce a new collection which combines all,
* in tuples. For instance:
*
* Vector.zip(Vector.of(1,2,3), ["a","b","c"], LinkedList.of(8,9,10))
* => Vector.of([1,"a",8], [2,"b",9], [3,"c",10])
*
* The result collection will have the length of the shorter
* of the input iterables. Extra elements will be discarded.
*
* Also see [the non-static version](#zip), which only combines two
* collections.
* @param A A is the type of the tuple that'll be generated
* (`[number,string,number]` for the code sample)
*/
static zip<A extends any[]>(...iterables: IterableArray<A>): Vector<A> {
let r = <L.List<A>>L.empty();
const iterators = iterables.map(i => i[Symbol.iterator]());
let items = iterators.map(i => i.next());
while (!items.some(item => item.done)) {
r = L.append<A>(<any>items.map(item => item.value), r);
items = iterators.map(i => i.next());
}
return new Vector(r);
}
/**
* Combine this collection with the collection you give in
* parameter to produce a new collection which combines both,
* in pairs. For instance:
*
* Vector.of(1,2,3).zip(["a","b","c"])
* => Vector.of([1,"a"], [2,"b"], [3,"c"])
*
* The result collection will have the length of the shorter
* of both collections. Extra elements will be discarded.
*
* Also see [[Vector.zip]] (static version which can more than two
* iterables)
*/
zip<U>(other: Iterable<U>): Vector<[T,U]> {
let r = <L.List<[T,U]>>L.empty();
const thisIterator = this[Symbol.iterator]();
const otherIterator = other[Symbol.iterator]();
let thisCurItem = thisIterator.next();
let otherCurItem = otherIterator.next();
while (!thisCurItem.done && !otherCurItem.done) {
r = L.append<[T,U]>([thisCurItem.value, otherCurItem.value], r);
thisCurItem = thisIterator.next();
otherCurItem = otherIterator.next();
}
return new Vector(r);
}
/**
* Reverse the collection. For instance:
*
* Vector.of(1,2,3).reverse();
* => Vector.of(3,2,1)
*/
reverse(): Vector<T> {
return new Vector(L.reverse(this._list));
}
/**
* Combine this collection with the index of the elements
* in it. Handy if you need the index when you map on
* the collection for instance:
*
* Vector.of("a","b").zipWithIndex().map(([v,idx]) => v+idx)
* => Vector.of("a0", "b1")
*/
zipWithIndex(): Vector<[T,number]> {
return <Vector<[T,number]>>SeqHelpers.zipWithIndex<T>(this);
}
/**
* Returns a new collection, discarding the elements
* after the first element which fails the predicate.
*/
takeWhile(predicate:(x:T)=>boolean): Vector<T> {
return new Vector(L.takeWhile(predicate, this._list));
}
/**
* Returns a new collection, discarding the elements
* after the first element which fails the predicate,
* but starting from the end of the collection.
*
* Vector.of(1,2,3,4).takeRightWhile(x => x > 2)
* => Vector.of(3,4)
*/
takeRightWhile(predicate:(x:T)=>boolean): Vector<T> {
return new Vector(L.takeLastWhile(predicate, this._list));
}
/**
* Split the collection at a specific index.
*
* Vector.of(1,2,3,4,5).splitAt(3)
* => [Vector.of(1,2,3), Vector.of(4,5)]
*/
splitAt(index:number): [Vector<T>,Vector<T>] {
if (index < 0) {
return [Vector.empty<T>(), this];
}
return <[Vector<T>,Vector<T>]>L.splitAt(index, this._list).map(x => new Vector(x));
}
/**
* Takes a predicate; returns a pair of collections.
* The first one is the longest prefix of this collection
* which satisfies the predicate, and the second collection
* is the remainder of the collection.
*
* Vector.of(1,2,3,4,5,6).span(x => x <3)
* => [Vector.of(1,2), Vector.of(3,4,5,6)]
*/
span(predicate:(x:T)=>boolean): [Vector<T>,Vector<T>] {
// could be potentially faster using splitAt.
const first = this.takeWhile(predicate);
return [first, this.drop(first.length())];
}
/**
* Returns a new collection with the first
* n elements discarded.
* If the collection has less than n elements,
* returns the empty collection.
*/
drop(n:number): Vector<T> {
return new Vector(L.drop(n, this._list));
}
/**
* Return a new collection containing the first n
* elements from this collection
*
* Vector.of(1,2,3,4).take(2)
* => Vector.of(1,2)
*/
take(n:number): Vector<T> {
if (n<0) {
return Vector.empty<T>();
}
return new Vector(L.take(n, this._list));
}
/**
* Prepend an element at the beginning of the collection.
*/
prepend(elt: T): Vector<T> {
return new Vector(L.prepend(elt, this._list));
}
/**
* Prepend multiple elements at the beginning of the collection.
*/
prependAll(elts: Iterable<T>): Vector<T> {
return Vector.ofIterable(elts).appendAll(this);
}
/**
* Removes the first element matching the predicate
* (use [[Seq.filter]] to remove all elements matching a predicate)
*/
removeFirst(predicate: (v:T)=>boolean): Vector<T> {
const v1 = this.takeWhile(x => !predicate(x));
return v1.appendAll(this.drop(v1.length()+1));
}
/**
* Returns a new collection with the last
* n elements discarded.
* If the collection has less than n elements,
* returns the empty collection.
*/
dropRight(n:number): Vector<T> {
if (n>=this.length()) {
return Vector.empty<T>();
}
return new Vector(L.dropLast(n, this._list));
}
/**
* Returns a new collection, discarding the last elements
* until one element fails the predicate. All elements
* before that point are retained.
*/
dropRightWhile(predicate:(x:T)=>boolean): Vector<T> {
let i=this.length()-1;
for (;i>=0;i--) {
if (!predicate(<T>L.nth(i, this._list))) {
return this.take(i+1);
}
}
return Vector.empty<T>();
}
/**
* Get all the elements in the collection but the first one.
* If the collection is empty, return None.
*/
tail(): Option<Vector<T>> {
if (this.isEmpty()) {
return Option.none<Vector<T>>();
}
return Option.of(new Vector(L.tail(this._list)));
}
/**
* Reduces the collection to a single value by repeatedly
* calling the combine function.
* No starting value. The order in which the elements are
* passed to the combining function is undetermined.
*/
reduce(combine: (v1:T,v2:T)=>T): Option<T> {
return SeqHelpers.reduce(this, combine);
}
/**
* Compare values in the collection and return the smallest element.
* Returns Option.none if the collection is empty.
*
* also see [[Vector.minOn]]
*/
minBy(compare: (v1:T,v2:T)=>Ordering): Option<T> {
return SeqHelpers.minBy(this, compare);
}
/**
* Call the function you give for each value in the collection
* and return the element for which the result was the smallest.
* Returns Option.none if the collection is empty.
*
* Vector.of({name:"Joe", age:12}, {name:"Paula", age:6}).minOn(x=>x.age)
* => Option.of({name:"Paula", age:6})
*
* also see [[Vector.minBy]]
*/
minOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.minOn(this, getOrderable);
}
/**
* Compare values in the collection and return the largest element.
* Returns Option.none if the collection is empty.
*
* also see [[Vector.maxOn]]
*/
maxBy(compare: (v1:T,v2:T)=>Ordering): Option<T> {
return SeqHelpers.maxBy(this, compare);
}
/**
* Call the function you give for each value in the collection
* and return the element for which the result was the largest.
* Returns Option.none if the collection is empty.
*
* Vector.of({name:"Joe", age:12}, {name:"Paula", age:6}).maxOn(x=>x.age)
* => Option.of({name:"Joe", age:12})
*
* also see [[Vector.maxBy]]
*/
maxOn(getOrderable: ToOrderable<T>): Option<T> {
return SeqHelpers.maxOn(this, getOrderable);
}
/**
* Call the function you give for each element in the collection
* and sum all the numbers, return that sum.
* Will return 0 if the collection is empty.
*
* Vector.of(1,2,3).sumOn(x=>x)
* => 6
*/
sumOn(getNumber: (v:T)=>number): number {
return SeqHelpers.sumOn(this, getNumber);
}
/**
* Slides a window of a specific size over the sequence.
* Returns a lazy stream so memory use is not prohibitive.
*
* Vector.of(1,2,3,4,5,6,7,8).sliding(3)
* => Stream.of(Vector.of(1,2,3), Vector.of(4,5,6), Vector.of(7,8))
*/
sliding(count:number): Stream<Vector<T>> {
return <Stream<Vector<T>>>SeqHelpers.sliding(this, count);
}
/**
* Apply the function you give to all elements of the sequence
* in turn, keeping the intermediate results and returning them
* along with the final result in a list.
* The last element of the result is the final cumulative result.
*
* Vector.of(1,2,3).scanLeft(0, (soFar,cur)=>soFar+cur)
* => Vector.of(0,1,3,6)
*/
scanLeft<U>(init:U, fn:(soFar:U,cur:T)=>U): Vector<U> {
return new Vector(L.scan(fn, init, this._list));
}
/**
* Apply the function you give to all elements of the sequence
* in turn, keeping the intermediate results and returning them
* along with the final result in a list.
* The first element of the result is the final cumulative result.
*
* Vector.of(1,2,3).scanRight(0, (cur,soFar)=>soFar+cur)
* => Vector.of(6,5,3,0)
*/
scanRight<U>(init:U, fn:(cur:T,soFar:U)=>U): Vector<U> {
const r:U[] = [];
r.unshift(init);
let cur = init;
for (let i = this.length()-1; i>=0; i--) {
cur = fn(<T>L.nth(i, this._list), cur);
r.unshift(cur);
}
return Vector.ofIterable(r);
}
}