For AI agents: the complete documentation index is available at /byethrow/ja/llms.txt, the full documentation bundle is available at /byethrow/ja/llms-full.txt, and this page is available as Markdown at /byethrow/ja/guide/tutorial/chaining/pipe-basics.md.
  • 日本語
  • pipe の基本

    pipe は @praha/byethrow の中心となる関数です。 複数の処理を、上から下へ流れる読みやすいひとつのフローとしてつなげます。

    pipe とは?

    pipe は初期値を受け取り、一連の関数に順番に渡していきます。 各関数は、ひとつ前の関数の出力を受け取ります。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: string
    result
    =
    import Result
    Result
    .
    function pipe<number, number, number, string>(a: number, ab: (a: number) => number, bc: (b: number) => number, cd: (c: number) => string): string (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    5, // 5 から始める (
    x: number
    x
    ) =>
    x: number
    x
    + 1, // 5 + 1 = 6
    (
    x: number
    x
    ) =>
    x: number
    x
    * 2, // 6 * 2 = 12
    (
    x: number
    x
    ) => `Result: ${
    x: number
    x
    }`, // 'Result: 12'
    ); // 'Result: 12'

    なぜ pipe を使うのか?

    pipe を使わない場合、関数呼び出しをネストすることになり、内側から外側へ読まなければなりません。

    // ❌ 読みにくい: 内側から外側へ読む必要がある
    const 
    const result: string
    result
    =
    const format: (x: number) => string
    format
    (
    const multiply: (a: number, b: number) => number
    multiply
    (
    const add: (a: number, b: number) => number
    add
    (5, 1), 2));

    あるいは中間変数を導入することになり、冗長になります。

    // ❌ 冗長: 変数が多すぎる
    const 
    const step1: number
    step1
    =
    const add: (a: number, b: number) => number
    add
    (5, 1);
    const
    const step2: number
    step2
    =
    const multiply: (a: number, b: number) => number
    multiply
    (
    const step1: number
    step1
    , 2);
    const
    const result: string
    result
    =
    const format: (x: number) => string
    format
    (
    const step2: number
    step2
    );

    pipe を使えば、各ステップを実行される順番に並べて書けます。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // ✅ 明快: 上から下へ読める const
    const result: string
    result
    =
    import Result
    Result
    .
    function pipe<number, number, number, string>(a: number, ab: (a: number) => number, bc: (b: number) => number, cd: (c: number) => string): string (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    5, (
    x: number
    x
    ) =>
    const add: (a: number, b: number) => number
    add
    (
    x: number
    x
    , 1),
    (
    x: number
    x
    ) =>
    const multiply: (a: number, b: number) => number
    multiply
    (
    x: number
    x
    , 2),
    const format: (x: number) => string
    format
    ,
    );

    Result と組み合わせる

    pipe の真価は Result と組み合わせたときに発揮されます。 Result.map などの関数はカリー化されており、コールバックを渡して呼び出すと Result を受け取る関数を返します。これはまさに pipe が期待する形です。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, never>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<10, never>, Result.Result<number, never>, Result.Result<number, never>>(a: Result.Result<10, never>, ab: (a: Result.Result<10, never>) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => Result.Result<number, never>): Result.Result<number, never> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<10>(value: 10): Result.Result<10, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (10),
    import Result
    Result
    .
    function map<Result.Result<10, never>, number>(fn: (a: 10) => number): (result: Result.Result<10, never>) => Result.Result<number, never> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    x: 10
    x
    ) =>
    x: 10
    x
    * 2),
    import Result
    Result
    .
    function map<Result.Result<number, never>, number>(fn: (a: number) => number): (result: Result.Result<number, never>) => Result.Result<number, never> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    x: number
    x
    ) =>
    x: number
    x
    + 5),
    ); // { type: 'Success', value: 25 }

    失敗するとパイプラインは短絡する

    各関数は、Result が自分の扱う状態のときだけコールバックを実行します。 たとえば map は Success のときだけ値を変換します。あるステップが失敗すると、後続の成功側のステップはスキップされ、Failure がそのまま末尾まで流れていきます。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, "Too large">
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<10, never>, Result.Result<10, "Too large">, Result.Result<number, "Too large">, Result.Result<number, "Too large">>(a: Result.Result<10, never>, ab: (a: Result.Result<10, never>) => Result.Result<10, "Too large">, bc: (b: Result.Result<10, "Too large">) => Result.Result<number, "Too large">, cd: (c: Result.Result<number, "Too large">) => Result.Result<number, "Too large">): Result.Result<number, "Too large"> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<10>(value: 10): Result.Result<10, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (10),
    import Result
    Result
    .
    function andThen<Result.Result<10, never>, Result.Success<10> | Result.Failure<"Too large">>(fn: (a: 10) => Result.Success<10> | Result.Failure<"Too large">): (result: Result.Result<10, never>) => Result.Result<10, "Too large"> (+1 overload)

    Chains the next computation using the success value of a Result or ResultAsync . If the original result is a Failure , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

    @typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    ((
    x: 10
    x
    ) =>
    x: 10
    x
    > 5 ?
    import Result
    Result
    .
    function fail<"Too large">(error: "Too large"): Result.Result<never, "Too large"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('Too large') :
    import Result
    Result
    .
    function succeed<10>(value: 10): Result.Result<10, never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    (
    x: 10
    x
    )),
    import Result
    Result
    .
    function map<Result.Result<10, "Too large">, number>(fn: (a: 10) => number): (result: Result.Result<10, "Too large">) => Result.Result<number, "Too large"> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    x: 10
    x
    ) =>
    x: 10
    x
    * 2), // スキップされる
    import Result
    Result
    .
    function map<Result.Result<number, "Too large">, number>(fn: (a: number) => number): (result: Result.Result<number, "Too large">) => Result.Result<number, "Too large"> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    x: number
    x
    ) =>
    x: number
    x
    + 5), // スキップされる
    ); // { type: 'Failure', error: 'Too large' }

    そのため、ステップごとにエラーを確認する必要はありません。

    SuccessFailureステップの結果を切り替えると、Result の行き先が変わります
    Result.succeed(id),
    Result.andThen(validateId),
    Result.andThen(findUser),
    Result.map(toWelcomeMessage),
    Result.inspectError(console.error),スキップ
    結果Success

    非同期のステップ

    いずれかのステップが Promise を返すと、それ以降のパイプラインは非同期に実行され、pipe は Promise を返します。 await が必要なのは最終結果だけです。

    const 
    const result: Result.Result<string, "NotFound">
    result
    = await
    import Result
    Result
    .
    function pipe<Result.Result<"u123", never>, Result.ResultAsync<User, "NotFound">, Result.ResultAsync<string, "NotFound">>(a: Result.Result<"u123", never>, ab: (a: Result.Result<"u123", never>) => Result.ResultAsync<User, "NotFound">, bc: (b: Result.ResultAsync<User, "NotFound">) => Result.ResultAsync<string, "NotFound">): Result.ResultAsync<string, "NotFound"> (+25 overloads)

    Applies a sequence of functions to a value, from left to right.

    This utility enables function composition in a left-to-right manner. It supports up to 26 chained functions from A through Z.

    @typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function succeed<"u123">(value: "u123"): Result.Result<"u123", never> (+1 overload)

    Creates a Success result from a given value.

    Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

    @typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed();
    // Result.Result<void, never>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    succeed
    ('u123'),
    import Result
    Result
    .
    function andThen<Result.Result<"u123", never>, Result.ResultAsync<User, "NotFound">>(fn: (a: "u123") => Result.ResultAsync<User, "NotFound">): (result: Result.Result<"u123", never>) => Result.ResultAsync<User, "NotFound"> (+1 overload)

    Chains the next computation using the success value of a Result or ResultAsync . If the original result is a Failure , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

    @typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    (
    const fetchUser: (id: string) => Result.ResultAsync<User, "NotFound">
    fetchUser
    ), // 非同期
    import Result
    Result
    .
    function map<Result.ResultAsync<User, "NotFound">, string>(fn: (a: User) => string): (result: Result.ResultAsync<User, "NotFound">) => Result.ResultAsync<string, "NotFound"> (+1 overload)

    Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

    @typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(2),
    Result.map((x) => x * 10),
    );
    // { type: 'Success', value: 20 }
    @example

    Failure Case (unchanged)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.map((x) => x * 10),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    map
    ((
    user: User
    user
    ) =>
    user: User
    user
    .
    name: string
    name
    ), // 解決済みの user を受け取る
    ); // 型: Result.Result<string, 'NotFound'>

    この章で扱う関数

    以降のページでは、パイプラインの中で使える関数を紹介します。 これらは成功側と失敗側の対になっています。

    コールバックの役割成功時失敗時
    値を変換するmapmapError
    次の Result を返すandThenorElse
    Result を返すが、元の値は保持されるandThroughorThrough
    副作用を実行し、戻り値は無視されるinspectinspectError

    リファレンス

    API用途
    pipe(value, ...fns)値を一連の関数に順番に渡す