Skip to content

Commit

Permalink
New release: Pipe.add() method added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Dec 23, 2024
1 parent 97238af commit 3a97a07
Show file tree
Hide file tree
Showing 8 changed files with 1,279 additions and 85 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# IterTools Typescript Change Log

## v1.29.0 - 2024-12-24

### New features
* Pipe
* `add()`

## v1.28.1 - 2024-12-23

Documentation fixed.
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3553,7 +3553,11 @@ type PipeOperationSequence<TFlow extends any[]> =
TFlow extends [infer T1, infer T2, ...infer Rest]
? [PipeOperation<T1, T2>, ...PipeOperationSequence<[T2, ...Rest]>]
: [];
type Pipe<TFlow extends any[]> = PipeOperation<First<TFlow>, Last<TFlow>>
type Pipe<TFlow extends any[]> = PipeOperation<First<TFlow>, Last<TFlow>> & {
add: TFlow extends []
? <TInput, TOutput>(operation: PipeOperation<TInput, TOutput>) => Pipe<[TInput, TOutput]>
: <T>(operation: PipeOperation<Last<TFlow>, T>) => Pipe<[...TFlow, T]>;
};
```

Pipe creation function:
Expand Down Expand Up @@ -3601,6 +3605,28 @@ const result1 = pipe([1, 1, 2, 2, 3, 4, 5]); // 14
const result2 = pipe([1, 1, 1, 2, 2, 2]); // 5
```

Example with creating pipe using chain calls:
```typescript
import { createPipe } from "itertools-ts";

const pipe = createPipe()
.add(set.distinct<number>)
.add((input) => single.map(input, (x) => x**2))
.add((input) => single.filter(input, (x) => x < 10))
.add(reduce.toSum);

const result1 = pipe([1, 1, 2, 2, 3, 4, 5]); // 14
const result2 = pipe([1, 1, 1, 2, 2, 2]); // 5

// You can create a new pipe adding some operations
const extendedPipe = pipe
.add((x) => x * 2)
.add((x) => x + 1);

const result3 = extendedPipe([1, 1, 2, 2, 3, 4, 5]); // 29
const result4 = extendedPipe([1, 1, 1, 2, 2, 2]); // 11
```

Asynchronous pipe example:
```typescript
import { createPipe } from "itertools-ts";
Expand All @@ -3624,6 +3650,10 @@ const result1 = await asyncPipe(asyncInput1); // 14
// You can reuse the pipe
const asyncInput2 = [1, 1, 1, 2, 2, 2].map((x) => Promise.resolve(x));
const result4 = await asyncPipe(asyncInput2); // 5

// You can create a new pipe adding some asynchronous operations
const extendedAsyncPipe = asyncPipe.add(async (x) => (await x) * 2);
const result5 = await extendedAsyncPipe(asyncInput2); // 10
```

You can also use pipe for non-iterables:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itertools-ts",
"version": "1.28.1",
"version": "1.29.0",
"description": "Extended itertools port for TypeScript and JavaScript. Provides a huge set of functions for working with iterable collections (including async ones)",
"license": "MIT",
"repository": {
Expand Down
File renamed without changes.
Loading

0 comments on commit 3a97a07

Please sign in to comment.