Skip to content

Commit

Permalink
docs: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodie committed Dec 24, 2024
1 parent f024ce4 commit c0ffeef
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 35 deletions.
20 changes: 10 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@

#### Fixes

* add docs for .chain()
([7c6d89f](https://github.com/hoodie/iterators.ts/commit/7c6d89f75ae386b37ee8514583507f8864073f40))
- add docs for .chain()
([7c6d89f](https://github.com/hoodie/iterators.ts/commit/7c6d89f75ae386b37ee8514583507f8864073f40))

### [v0.1.2](https://github.com/hoodie/iterators.ts/compare/v0.1.1...v0.1.2) (2024-12-24)

#### Features

* add .chain()
([f4b2cb7](https://github.com/hoodie/iterators.ts/commit/f4b2cb729ca607d0ac6c60d150d1dab889e5a07f))
- add .chain()
([f4b2cb7](https://github.com/hoodie/iterators.ts/commit/f4b2cb729ca607d0ac6c60d150d1dab889e5a07f))

### [v0.1.1](https://github.com/hoodie/iterators.ts/compare/v0.1.0...v0.1.1) (2024-12-17)

#### Fixes

* add more documentation
([903fef4](https://github.com/hoodie/iterators.ts/commit/903fef446935a417f23d4a441d6d3fbb0849a662))
- add more documentation
([903fef4](https://github.com/hoodie/iterators.ts/commit/903fef446935a417f23d4a441d6d3fbb0849a662))

## v0.1.0 (2024-12-17)

### Features

* port to deno module
([cdf631e](https://github.com/hoodie/iterators.ts/commit/cdf631e802346065d542d6facf8132b35cce533a))
- port to deno module
([cdf631e](https://github.com/hoodie/iterators.ts/commit/cdf631e802346065d542d6facf8132b35cce533a))

### Fixes

* map must obey {done}
([2bf7b3d](https://github.com/hoodie/iterators.ts/commit/2bf7b3d03ed7e085da2260cb3c69e1ac83db1806))
- map must obey {done}
([2bf7b3d](https://github.com/hoodie/iterators.ts/commit/2bf7b3d03ed7e085da2260cb3c69e1ac83db1806))
142 changes: 118 additions & 24 deletions README.md
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]
```
3 changes: 2 additions & 1 deletion bumpversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ if (import.meta.main) {

await git("add", "deno.json", "deno.lock");
await git("commit", "-m", "chore: bump version");

await git("tag", `v${next_version}`);

await convco("changelog", "-o", "CHANGELOG.md");
await run("deno", { args: ["fmt"] });

await git("add", "CHANGELOG.md");
await git("commit", "-m", "chore: changelog");
Expand Down

0 comments on commit c0ffeef

Please sign in to comment.