Skip to content

関数合成

pipeについて紹介します。

pipeという関数は、すでにいくつかのライブラリで提供されているので、馴染みがあるかもしれません。

pipeは、関数の出力を別の関数の引数に渡すことで、関数をつなげる関数です。

pipeがなぜ必要なのか、少し見てみましょう。

配列があり、この配列にfilter -> map -> reduceを実行して最終結果を得たいとします。

typescript
const sum = (a: number, b: number) => a + b;
const arr = [1, 2, 3, 4, 5];
// filter
// map
// reduce

すべてのコードが純粋関数なので、関数合成を作成するのは簡単ですが、非常に読みにくく見えます。

typescript
reduce(
  sum,
  map(
    (a) => a + 10,
    filter((a) => a % 2 === 0, arr);
  ),
)

上記の問題を解決するために、pipeを提供しています。

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

pipe(
  arr,
  filter((a) => a % 2 === 0),
  map((a) => a + 10),
  reduce(sum),
);

pipeと一緒に使うと読みやすくなります。

Array.prototype.[Function]との比較については、この記事を確認してください。


また、Promiseの値を直接扱う必要がありません。

typescript
await pipe(
  Promise.resolve(1),
  (a /*: Awaited<number>*/) => a + 1,
  async (b /*: Awaited<number>*/) => b + 1,
  (c /*: Awaited<number>*/) => c + 1,
); // 4
  • a : aは number として推論され、実際の値もPromise<number>ではなく number です。
  • c : 前の関数が非同期関数でも、引数はPromise<number>ではありません。

非同期値を直接扱わなくても、エラー発生時に処理できないわけではありません。 エラー処理については、エラー処理を確認してください。

Released under the Apache-2.0 License.