<A, B, X>(f: (input: A, i: number) => Result<B, X>): (
self: Iterable<A>
) => Iterable<B>
<A, B, X>(
self: Iterable<A>,
f: (input: A, i: number) => Result<B, X>
): Iterable<B>Transforms all elements of the Iterable for as long as the specified function succeeds.
Example (Filtering and transforming until failure)
import { Iterable, Result } from "effect"
// Parse numbers until we hit an invalid one
const strings = ["1", "2", "3", "invalid", "4", "5"]
const numbers = Iterable.filterMapWhile(strings, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Array.from(numbers)) // [1, 2, 3] (stops at "invalid")
// Take elements while they meet a condition and transform them
const values = [2, 4, 6, 7, 8, 10]
const doubledEvens = Iterable.filterMapWhile(
values,
(n) => n % 2 === 0 ? Result.succeed(n * 2) : Result.failVoid
)
console.log(Array.from(doubledEvens)) // [4, 8, 12] (stops at 7)
// Process with index until condition fails
const letters = ["a", "b", "c", "d", "e"]
const indexedUntilC = Iterable.filterMapWhile(
letters,
(letter, i) => letter !== "c" ? Result.succeed(`${i}: ${letter}`) : Result.failVoid
)
console.log(Array.from(indexedUntilC)) // ["0: a", "1: b"] (stops at "c")export const const filterMapWhile: {
<A, B, X>(
f: (input: A, i: number) => Result<B, X>
): (self: Iterable<A>) => Iterable<B>
<A, B, X>(
self: Iterable<A>,
f: (input: A, i: number) => Result<B, X>
): Iterable<B>
}
Transforms all elements of the Iterable for as long as the specified function succeeds.
Example (Filtering and transforming until failure)
import { Iterable, Result } from "effect"
// Parse numbers until we hit an invalid one
const strings = ["1", "2", "3", "invalid", "4", "5"]
const numbers = Iterable.filterMapWhile(strings, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Array.from(numbers)) // [1, 2, 3] (stops at "invalid")
// Take elements while they meet a condition and transform them
const values = [2, 4, 6, 7, 8, 10]
const doubledEvens = Iterable.filterMapWhile(
values,
(n) => n % 2 === 0 ? Result.succeed(n * 2) : Result.failVoid
)
console.log(Array.from(doubledEvens)) // [4, 8, 12] (stops at 7)
// Process with index until condition fails
const letters = ["a", "b", "c", "d", "e"]
const indexedUntilC = Iterable.filterMapWhile(
letters,
(letter, i) => letter !== "c" ? Result.succeed(`${i}: ${letter}`) : Result.failVoid
)
console.log(Array.from(indexedUntilC)) // ["0: a", "1: b"] (stops at "c")
filterMapWhile: {
<function (type parameter) A in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>A, function (type parameter) B in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>B, function (type parameter) X in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>X>(f: (input: A, i: number) => Result<B, X>f: (input: Ainput: function (type parameter) A in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>A, i: numberi: number) => type Result<A, E = never> = R.Success<A, E> | R.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) B in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>B, function (type parameter) X in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>X>): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>A>) => interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>B>
<function (type parameter) A in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>A, function (type parameter) B in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>B, function (type parameter) X in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>X>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>A>, f: (input: A, i: number) => Result<B, X>f: (input: Ainput: function (type parameter) A in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>A, i: numberi: number) => type Result<A, E = never> = R.Success<A, E> | R.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) B in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>B, function (type parameter) X in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>X>): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>B>
} = import dualdual(2, <function (type parameter) A in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): {
[Symbol.iterator](): {
next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
};
};
}
A, function (type parameter) B in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): {
[Symbol.iterator](): {
next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
};
};
}
B, function (type parameter) X in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): {
[Symbol.iterator](): {
next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
};
};
}
X>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): {
[Symbol.iterator](): {
next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
};
};
}
A>, f: (input: A, i: number) => Result<B, X>f: (input: Ainput: function (type parameter) A in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): {
[Symbol.iterator](): {
next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
};
};
}
A, i: numberi: number) => type Result<A, E = never> = R.Success<A, E> | R.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) B in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): {
[Symbol.iterator](): {
next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
};
};
}
B, function (type parameter) X in <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): {
[Symbol.iterator](): {
next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
};
};
}
X>) => ({
[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]() {
const const iterator: Iterator<A, any, any>iterator = self: Iterable<A>self[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]()
let let i: numberi = 0
return {
function next(): {
done: boolean;
value: undefined;
} | {
done: boolean;
value: B;
}
next() {
const const result: IteratorResult<A, any>result = const iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
if (const result: IteratorResult<A, any>result.done?: boolean | undefineddone) {
return { done: booleandone: true, value: undefinedvalue: var undefinedundefined }
}
const const next: Result<B, X>next = f: (input: A, i: number) => Result<B, X>f(const result: IteratorYieldResult<A>result.IteratorYieldResult<A>.value: Avalue, let i: numberi++)
if (import RR.const isSuccess: <A, E>(
self: Result<A, E>
) => self is Success<A, E>
Checks whether a Result is a Success.
When to use
Use to narrow a known Result to the Success variant.
Details
- Acts as a TypeScript type guard, narrowing to
Success<A, E>
- After narrowing, you can access
.success to read the value
Example (Narrowing to success)
import { Result } from "effect"
const result = Result.succeed(42)
if (Result.isSuccess(result)) {
console.log(result.success)
// Output: 42
}
isSuccess(const next: Result<B, X>next)) {
return { done: booleandone: false, value: Bvalue: const next: R.Success<B, X>const next: {
_tag: "Success";
_op: "Success";
success: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
next.Success<B, X>.success: Bsuccess }
}
return { done: booleandone: true, value: undefinedvalue: var undefinedundefined }
}
}
}
}))