Skip to content

Commit

Permalink
docs: update lazy evaluation example
Browse files Browse the repository at this point in the history
  • Loading branch information
ppeeou committed Nov 26, 2021
1 parent 7e59a64 commit 0a27333
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
5 changes: 2 additions & 3 deletions website/docs_md/function-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ A function named `pipe` is already provided by several libraries, so you may be

`pipe` is a function that connects functions by passing the output of a function to the arguments of another function.

<br/>

Let's take a moment to see why we need `pipe`.

There is an array, and you want to get the final result by doing `filter` -> `map` -> `reduce` on this array.
Expand Down Expand Up @@ -43,7 +41,7 @@ reduce(
We are providing `pipe` to solve the above problem.

```typescript
import { pipe, filter, map } from "@fxts/core";
import { pipe, filter, map, reduce } from "@fxts/core";

pipe(
arr,
Expand All @@ -58,6 +56,7 @@ It looks easy to read when used with `pipe`.
> Check out [this article](https://fxts.dev/docs/lazy-evaluation) for a comparison with `Array.prototype.[Function]`
<br/>

Also, you don't have to deal with `Promise` values directly.

```typescript
Expand Down
38 changes: 19 additions & 19 deletions website/docs_md/lazy-evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,28 @@ Lazy functions can be found [here](https://fxts.dev/docs/index#lazy)
The code below shows a more useful situation.

```typescript
// {userId: number, id: number, title: string, complate: boolean}
const fetchTodo = async (page: number) =>
fetch(`https://jsonplaceholder.typicode.com/todos/${page}`);

const todoLists = async (pages: Iterable<number>) =>
/**
* [{
* title: string,
* director: string,
* language: string,
* genre: string,
* rating: number,
* ...
* }]
*/
const fetchMovie = async (year: number) =>
fetch(`https://api.movie.xxx/${year}`);

const recommendMovie = async (year: number, rating: number) =>
pipe(
pages,
range(year, Infinity),
toAsync,
map(fetchTodo),
map(fetchMovie),
map((res) => res.json()),
filter((movie) => movie.rating >= rating),
head,
);

const wordCount = async (start: number, end: number) =>
pipe(
range(start, end),
todoLists,
// // If you want to get only complated todo / Composition of Functions is easy
// filter((todo) => todo.completed),
map((todo) => todo.title.split(" ")),
flat,
countBy((word) => word),
);

await wordCount(1, 5);
await recommendMovie(2020, 9);
```

0 comments on commit 0a27333

Please sign in to comment.