#Pipe Basics
pipe is the heart of @praha/byethrow.
It connects multiple operations into a single, readable flow that runs from top to bottom.
#What Is pipe?
pipe takes an initial value and passes it through a series of functions.
Each function receives the output of the previous one:
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.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
5, // Start with 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'#Why Use pipe?
Without pipe, you either nest function calls, which must be read from the inside out:
// ❌ Hard to read: inside out
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));Or you introduce intermediate variables, which is verbose:
// ❌ Verbose: too many variables
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 );With pipe, the steps are listed in the order they run:
import { import Result Result } from '@praha/byethrow';
// ✅ Clear: top to bottom
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.@exampleimport { 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 ,
);#Using pipe with Results
The real power of pipe shows when working with Results.
Functions such as Result.map are curried: calling them with a callback returns a function that takes a Result, which is exactly what pipe expects:
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.@exampleimport { 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.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith 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.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@exampleFailure 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.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@exampleFailure 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 }#Failures Short-Circuit the Pipeline
Each function only runs its callback when the Result is in the state it cares about.
For example, map only transforms a Success. Once a step fails, the following success-side steps are skipped and the Failure flows straight to the end:
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.@exampleimport { 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.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith 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.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.succeed(x * 2)),
);
// { type: 'Success', value: 6 }
@exampleFailure 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' }
@exampleFailure 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.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith 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.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith 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.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@exampleFailure 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), // Skipped
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.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@exampleFailure 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), // Skipped
);
// { type: 'Failure', error: 'Too large' }This is why you don't need to check for errors after every step.
Result.succeed(id),Result.andThen(validateId),Result.andThen(findUser),Result.map(toWelcomeMessage),Result.inspectError(console.error),skipped#Async Steps
If any step returns a Promise, the rest of the pipeline runs asynchronously and pipe returns a Promise.
You only need to await the final result:
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.@exampleimport { 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.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith 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.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.succeed(x * 2)),
);
// { type: 'Success', value: 6 }
@exampleFailure 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' }
@exampleFailure 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 ), // async
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.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@exampleFailure 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 ), // receives the resolved user
);
// Type: Result.Result<string, 'NotFound'>#Functions in This Chapter
The following pages cover the functions you can use inside a pipeline. They come in pairs: one for the success side and one for the failure side.
| What the callback does | On success | On failure |
|---|---|---|
| Transforms the value | map | mapError |
Returns the next Result | andThen | orElse |
Returns a Result, but the original value is kept | andThrough | orThrough |
| Runs a side effect and its return value is ignored | inspect | inspectError |
#References
| API | Purpose |
|---|---|
| pipe(value, ...fns) | Pass a value through a series of functions |
