From 095ef5591fba74b86e904fa6ca35c46ad88ef83b Mon Sep 17 00:00:00 2001 From: hw Date: Tue, 26 Nov 2024 00:45:01 +0900 Subject: [PATCH 1/2] feat: support mapEffect --- src/Lazy/fx.ts | 18 ++++++++++++++++++ src/Lazy/index.ts | 1 + src/Lazy/map.ts | 5 +++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Lazy/fx.ts b/src/Lazy/fx.ts index f7f511e2..ac8a1a34 100644 --- a/src/Lazy/fx.ts +++ b/src/Lazy/fx.ts @@ -66,6 +66,15 @@ class FxAsyncIterable { return new FxAsyncIterable(map(f, this.asyncIterable)); } + /** + * A function that is identical to `map`, but is intended to create side effects as part of its convention. + * + * see {@link https://fxts.dev/docs/map | map} + */ + mapEffect(f: (a: A) => B) { + return new FxAsyncIterable(map(f, this.asyncIterable)); + } + /** * Returns flattened AsyncIterable of values by running each element * flattening the mapped results. @@ -366,6 +375,15 @@ export class FxIterable { return new FxIterable(map(f, this.iterable)); } + /** + * A function that is identical to `map`, but is intended to create side effects as part of its convention. + * + * see {@link https://fxts.dev/docs/map | map} + */ + mapEffect(f: (a: A) => B): FxIterable { + return new FxIterable(map(f, this.iterable)); + } + /** * Returns flattened Iterable of values by running each element * flattening the mapped results. diff --git a/src/Lazy/index.ts b/src/Lazy/index.ts index 2f67d87d..9b876658 100644 --- a/src/Lazy/index.ts +++ b/src/Lazy/index.ts @@ -66,6 +66,7 @@ export { intersectionBy, keys, map, + map as mapEffect, peek, pipeLazy, pluck, diff --git a/src/Lazy/map.ts b/src/Lazy/map.ts index 30749182..6b67df18 100644 --- a/src/Lazy/map.ts +++ b/src/Lazy/map.ts @@ -1,5 +1,4 @@ -import { AsyncFunctionException } from "../_internal/error"; -import { isAsyncIterable, isIterable, isPromise } from "../_internal/utils"; +import { isAsyncIterable, isIterable } from "../_internal/utils"; import type Awaited from "../types/Awaited"; import type IterableInfer from "../types/IterableInfer"; import type ReturnIterableIteratorType from "../types/ReturnIterableIteratorType"; @@ -55,6 +54,8 @@ function async( /** * Returns Iterable/AsyncIterable of values by running each applying `f`. * + * If the `map` causes side effects, it is recommended to use `mapEffect` instead. + * * @example * ```ts * const iter = map(a => a + 10, [1, 2, 3, 4]); From ba03a7861c0d74607e17ed8bea1dab1b06fb7f88 Mon Sep 17 00:00:00 2001 From: hw Date: Tue, 26 Nov 2024 00:45:24 +0900 Subject: [PATCH 2/2] docs: update other alias function --- src/head.ts | 4 +++- src/includes.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/head.ts b/src/head.ts index 9517e017..cb6d95ed 100644 --- a/src/head.ts +++ b/src/head.ts @@ -14,7 +14,9 @@ type HeadReturnType = T extends readonly [a: infer H, ...rest: any[]] : never; /** - * Returns the first element of Iterable/AsyncIterable. (head) + * Returns the first element of Iterable/AsyncIterable. + * + * There is another alias function called `first`. * * @example * ```ts diff --git a/src/includes.ts b/src/includes.ts index 25199248..500dc2b4 100644 --- a/src/includes.ts +++ b/src/includes.ts @@ -3,7 +3,9 @@ import some from "./some"; import type ReturnValueType from "./types/ReturnValueType"; /** - * Checks if the specified value is equal. (contains) + * Checks if the specified value is equal. + * + * There is another alias function called `contains`. * * @example * ```ts