Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
6d8eef663a | |||
03f050bcfb |
4 changed files with 34 additions and 0 deletions
|
@ -58,6 +58,8 @@ isum(i1); // 6
|
||||||
icat(iarray(["a", "b", "c"])); // abc
|
icat(iarray(["a", "b", "c"])); // abc
|
||||||
imin(i1); // 1
|
imin(i1); // 1
|
||||||
imax(i1); // 3
|
imax(i1); // 3
|
||||||
|
iminBy(i1, (x) => -x); // 3
|
||||||
|
imaxBy(i1, (x) => -x); // 1
|
||||||
|
|
||||||
// Transform iterables
|
// Transform iterables
|
||||||
iloop(i1); // 1 2 3 1 2 3 ...
|
iloop(i1); // 1 2 3 1 2 3 ...
|
||||||
|
@ -67,6 +69,7 @@ ifilter(i3, (x) => x % 4 == 0); // 0 -4 -8 -12 ...
|
||||||
imapfilter(i1, (x) => x != 4 ? x * 2 : null); // 2 6
|
imapfilter(i1, (x) => x != 4 ? x * 2 : null); // 2 6
|
||||||
ipartition(i3, (x) => x % 4 == 0); // [0 -4 -8 ..., -1 -2 -3 -5 ...]
|
ipartition(i3, (x) => x % 4 == 0); // [0 -4 -8 ..., -1 -2 -3 -5 ...]
|
||||||
iunique(iarray([1, 4, 2, 4, 3, 1, 5])); // 1 4 2 3 5
|
iunique(iarray([1, 4, 2, 4, 3, 1, 5])); // 1 4 2 3 5
|
||||||
|
ienumerate(iarray(["a", "b", "c"])); // [0, "a"] [1, "b"] [2, "c"]
|
||||||
|
|
||||||
// Combine iterables
|
// Combine iterables
|
||||||
ichain(i1, i2); // 1 2 3 4
|
ichain(i1, i2); // 1 2 3 4
|
||||||
|
|
|
@ -24,6 +24,8 @@ isum(i1); // 6
|
||||||
icat(iarray(["a", "b", "c"])); // abc
|
icat(iarray(["a", "b", "c"])); // abc
|
||||||
imin(i1); // 1
|
imin(i1); // 1
|
||||||
imax(i1); // 3
|
imax(i1); // 3
|
||||||
|
iminBy(i1, (x) => -x); // 3
|
||||||
|
imaxBy(i1, (x) => -x); // 1
|
||||||
|
|
||||||
// Transform iterables
|
// Transform iterables
|
||||||
iloop(i1); // 1 2 3 1 2 3 ...
|
iloop(i1); // 1 2 3 1 2 3 ...
|
||||||
|
@ -33,6 +35,7 @@ ifilter(i3, (x) => x % 4 == 0); // 0 -4 -8 -12 ...
|
||||||
imapfilter(i1, (x) => x != 4 ? x * 2 : null); // 2 6
|
imapfilter(i1, (x) => x != 4 ? x * 2 : null); // 2 6
|
||||||
ipartition(i3, (x) => x % 4 == 0); // [0 -4 -8 ..., -1 -2 -3 -5 ...]
|
ipartition(i3, (x) => x % 4 == 0); // [0 -4 -8 ..., -1 -2 -3 -5 ...]
|
||||||
iunique(iarray([1, 4, 2, 4, 3, 1, 5])); // 1 4 2 3 5
|
iunique(iarray([1, 4, 2, 4, 3, 1, 5])); // 1 4 2 3 5
|
||||||
|
ienumerate(iarray(["a", "b", "c"])); // [0, "a"] [1, "b"] [2, "c"]
|
||||||
|
|
||||||
// Combine iterables
|
// Combine iterables
|
||||||
ichain(i1, i2); // 1 2 3 4
|
ichain(i1, i2); // 1 2 3 4
|
||||||
|
|
10
mod.test.ts
10
mod.test.ts
|
@ -8,6 +8,7 @@ import {
|
||||||
ichainit,
|
ichainit,
|
||||||
icombine,
|
icombine,
|
||||||
IEMPTY,
|
IEMPTY,
|
||||||
|
ienumerate,
|
||||||
ifilter,
|
ifilter,
|
||||||
ifilterclass,
|
ifilterclass,
|
||||||
ifiltertype,
|
ifiltertype,
|
||||||
|
@ -286,6 +287,15 @@ describe("Iterators", () => {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("enumerate iterators", () => {
|
||||||
|
let iterator = ienumerate(iarray(["a", "b", "c"]));
|
||||||
|
checkit(iterator, [
|
||||||
|
[0, "a"],
|
||||||
|
[1, "b"],
|
||||||
|
[2, "c"],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it("zips iterators", () => {
|
it("zips iterators", () => {
|
||||||
checkit(izip(IEMPTY, IEMPTY), []);
|
checkit(izip(IEMPTY, IEMPTY), []);
|
||||||
checkit(izip(iarray([1, 2, 3]), iarray(["a", "b"])), [[1, "a"], [
|
checkit(izip(iarray([1, 2, 3]), iarray(["a", "b"])), [[1, "a"], [
|
||||||
|
|
18
mod.ts
18
mod.ts
|
@ -390,6 +390,24 @@ export function icombine<T1, T2>(
|
||||||
return ichainit(imap(it1, (v1) => imap(it2, (v2): [T1, T2] => [v1, v2])));
|
return ichainit(imap(it1, (v1) => imap(it2, (v2): [T1, T2] => [v1, v2])));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enumerate another iterable, adding the index before the value.
|
||||||
|
*/
|
||||||
|
export function ienumerate<T>(it: Iterable<T>): Iterable<[number, T]> {
|
||||||
|
return {
|
||||||
|
[Symbol.iterator]: function* () {
|
||||||
|
const iterator = it[Symbol.iterator]();
|
||||||
|
let state = iterator.next();
|
||||||
|
let idx = 0;
|
||||||
|
while (!state.done) {
|
||||||
|
yield [idx, state.value];
|
||||||
|
state = iterator.next();
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Advance through two iterables at the same time, yielding item pairs
|
* Advance through two iterables at the same time, yielding item pairs
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue