-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,126 @@ | ||
# Lazy Iterators in Typescript | ||
|
||
Javascript iterators suck so... | ||
# Lazy Iterators | ||
|
||
```typescript | ||
const it = Iter.fromArray([ | ||
"a", | ||
"b", | ||
"c", | ||
"d", | ||
"e", | ||
"f", | ||
"g", | ||
"h", | ||
"i", | ||
"j", | ||
"k", | ||
"l", | ||
const result = Iter.fromArray([ | ||
"apple", | ||
"banana", | ||
"cherry", | ||
"date", | ||
"elderberry", | ||
]) | ||
.enumerate() | ||
.map(([k, v]) => [k, v.toUpperCase()]) | ||
.filter(([k, v]) => k % 2 == 0) | ||
.map(([, v]) => v); | ||
.map(([index, value]) => [index, value.toUpperCase()]) | ||
.filter(([index, value]) => index % 2 === 0) | ||
.take(2) | ||
.intoArray(); | ||
``` | ||
|
||
This is now published under <https://jsr.io/@hoodie/iterators>. | ||
|
||
## Examples | ||
|
||
const collected = it.take(3) | ||
.intoArray(); // -> [ 'A', 'C', 'E'] | ||
### Basic Usage | ||
|
||
const collected2 = it | ||
.intoArray(); // -> ['G', 'I', 'K'] | ||
Create an iterator from an array and collect all elements into an array. | ||
|
||
```ts | ||
const array = [1, 2, 3, 4, 5]; | ||
const iterator = Iter.fromArray(array); | ||
const result = iterator.intoArray(); | ||
console.log(result); // [1, 2, 3, 4, 5] | ||
``` | ||
|
||
This is now published under <https://jsr.io/@hoodie/iterators>. | ||
### Enumerate | ||
|
||
Create an iterator which yields the current index and the value of the original | ||
iterator. | ||
|
||
```ts | ||
const array = ["a", "b", "c"]; | ||
const iterator = Iter.fromArray(array).enumerate(); | ||
const result = iterator.intoArray(); | ||
console.log(result); // [[0, 'a'], [1, 'b'], [2, 'c']] | ||
``` | ||
|
||
### Filter | ||
|
||
Create an iterator skipping all elements which don't meet the given predicate. | ||
|
||
```ts | ||
const array = [1, 2, 3, 4, 5]; | ||
const iterator = Iter.fromArray(array).filter((x) => x % 2 === 0); | ||
const result = iterator.intoArray(); | ||
console.log(result); // [2, 4] | ||
``` | ||
|
||
### Find | ||
|
||
Find the first element that satisfies the predicate. | ||
|
||
```ts | ||
const array = [1, 2, 3, 4, 5]; | ||
const iterator = Iter.fromArray(array); | ||
const result = iterator.find((x) => x > 3); | ||
console.log(result); // 4 | ||
``` | ||
|
||
### Map | ||
|
||
Create an iterator applying the given callback to each element. | ||
|
||
```ts | ||
const array = [1, 2, 3]; | ||
const iterator = Iter.fromArray(array).map((x) => x * 2); | ||
const result = iterator.intoArray(); | ||
console.log(result); // [2, 4, 6] | ||
``` | ||
|
||
### Skip | ||
|
||
Create an iterator skipping the first `limit` elements. | ||
|
||
```ts | ||
const array = [1, 2, 3, 4, 5]; | ||
const iterator = Iter.fromArray(array).skip(2); | ||
const result = iterator.intoArray(); | ||
console.log(result); // [3, 4, 5] | ||
``` | ||
|
||
### Take | ||
|
||
Create an iterator yielding the first `limit` elements. | ||
|
||
```ts | ||
const array = [1, 2, 3, 4, 5]; | ||
const iterator = Iter.fromArray(array).take(3); | ||
const result = iterator.intoArray(); | ||
console.log(result); // [1, 2, 3] | ||
``` | ||
|
||
### Zip | ||
|
||
Create an iterator that ‘Zips up’ two iterators into a single iterator of pairs. | ||
|
||
```ts | ||
const array1 = [1, 2, 3]; | ||
const array2 = ["a", "b", "c"]; | ||
const iterator1 = Iter.fromArray(array1); | ||
const iterator2 = Iter.fromArray(array2); | ||
const zipped = iterator1.zip(iterator2); | ||
const result = zipped.intoArray(); | ||
console.log(result); // [[1, 'a'], [2, 'b'], [3, 'c']] | ||
``` | ||
|
||
### Cycle | ||
|
||
Create an iterator that cycles through the elements of the original iterator. | ||
|
||
```ts | ||
const array = [1, 2, 3]; | ||
const iterator = Iter.fromArray(array).cycle(); | ||
const result = []; | ||
for (let i = 0; i < 10; i++) { | ||
result.push(iterator.next().value); | ||
} | ||
console.log(result); // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters