Skip to content

Commit

Permalink
docs: add fx
Browse files Browse the repository at this point in the history
  • Loading branch information
ppeeou committed Mar 10, 2024
1 parent ee3d1f3 commit d0915ed
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
81 changes: 81 additions & 0 deletions website/docs_md/method-chaining.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
id: method-chaining
---

# Method Chaining

You can handle Iterable/AsyncIterable through a [pipe](https://fxts.dev/docs/pipe), but `fxts` also provides data change in the form of method chaining.

```ts
fx([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
.filter((a) => a % 2 === 0) // [0, 2]
.map((a) => a * a) // [0, 4]
.take(2) // [0, 4]
.reduce(sum); // 4

fx("abc")
.map((a) => a.toUpperCase()) // ["a", "b"]
.take(2)
.toArray(); // ["a", "b"]
```

### Note

Since `fx` defaults to lazy evaluation, it is not actually evaluated until strict evaluation methods such as `toArray`, `groupBy`, `indexBy`, and `some` are executed.

For details on lazy evaluation, please refer to https://fxts.dev/docs/lazy-evaluation.

### Support for handling AsyncIterable

`fx` can also handle [AsyncIterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator) values. `toAsync` is used in the example below to create an `AsyncIterator` value.

```ts
await fx(toAsync([1, 2, 3, 4]))
.filter(async (a) => a % 2 === 0)
.map(async (a) => a * a)
.reduce(sum);

await fx([1, 2, 3, 4])
.filter((a) => a % 2 === 0)
.toAsync() // if async function returns
.map(async (a) => a * a)
.reduce(sum);
```

### Handle Concurrency

`fx` supports concurrent operation. As we saw in concurrent, concurrent can only be used in asyncIterable.

For details on handling concurrent with `fxts`, please refer to https://fxts.dev/docs/handle-concurrency

```ts
/**
*
* evaluation
* ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
* │ 1 │──│ 2 │──│ 3 │──│ 4 │──│ 5 │──│ 6 │
* └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘ └──┬──┘
* map │ │ │ │ │ │
* concurrent(3) (1) (1) (2) (2) (3) (3)
* │ │ │ │ │ │
* ▼ ▼ ▼ ▼ ▼ ▼
*/
await fx(toAsync(range(1, 7)))
// async function returns
.map(async (a) => delay(100, a))
.concurrent(2)
.consume(); // It takes approximately 300ms.
```

### Etc

`fx` does not provide all the functions of `fxts` as methods.

If you want to use the `fxts` function which is not provided or additional functions, you can use the `chain` method.

```ts
fx([1, 2, 3, 4])
.chain(append(5))
.map((a) => a + 10)
.toArray(); // [11, 12, 13, 14, 15]
```
1 change: 1 addition & 0 deletions website/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"filter",
"flat",
"flatMap",
"fx",
"intersection",
"intersectionBy",
"keys",
Expand Down
5 changes: 5 additions & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ const sidebars = {
id: "error-handling",
label: "Error handling",
},
{
type: "doc",
id: "method-chaining",
label: "Method Chaining",
},
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion website/tsdoc-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"toolPackages": [
{
"packageName": "@microsoft/api-extractor",
"packageVersion": "7.18.19"
"packageVersion": "7.42.3"
}
]
}

0 comments on commit d0915ed

Please sign in to comment.