<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 elements of an iterable using a function that returns a Result, keeping only successful values.
Details
This combines mapping and filtering in a single operation. The function is
applied to each element, and only elements that result in Result.succeed
are included in the result.
Example (Filtering and transforming Result values)
import { Iterable, Result } from "effect"
// Parse strings to numbers, keeping only valid ones
const strings = ["1", "2", "invalid", "4", "not-a-number"]
const numbers = Iterable.filterMap(strings, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Array.from(numbers)) // [1, 2, 4]
// Extract specific properties from objects
const users = [
{ name: "Alice", age: 25, email: "[email protected]" },
{ name: "Bob", age: 17, email: undefined },
{ name: "Charlie", age: 30, email: "[email protected]" },
{ name: "David", age: 16, email: undefined }
]
const adultEmails = Iterable.filterMap(
users,
(user) =>
user.age >= 18 && user.email ? Result.succeed(user.email) : Result.failVoid
)
console.log(Array.from(adultEmails)) // ["[email protected]", "[email protected]"]
// Use index in transformation
const items = ["a", "b", "c", "d", "e"]
const evenIndexItems = Iterable.filterMap(
items,
(item, i) => i % 2 === 0 ? Result.succeed(`${i}: ${item}`) : Result.failVoid
)
console.log(Array.from(evenIndexItems)) // ["0: a", "2: c", "4: e"]export const const filterMap: {
<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 elements of an iterable using a function that returns a Result, keeping only successful values.
Details
This combines mapping and filtering in a single operation. The function is
applied to each element, and only elements that result in Result.succeed
are included in the result.
Example (Filtering and transforming Result values)
import { Iterable, Result } from "effect"
// Parse strings to numbers, keeping only valid ones
const strings = ["1", "2", "invalid", "4", "not-a-number"]
const numbers = Iterable.filterMap(strings, (s) => {
const num = parseInt(s)
return isNaN(num) ? Result.failVoid : Result.succeed(num)
})
console.log(Array.from(numbers)) // [1, 2, 4]
// Extract specific properties from objects
const users = [
{ name: "Alice", age: 25, email: "[email protected]" },
{ name: "Bob", age: 17, email: undefined },
{ name: "Charlie", age: 30, email: "[email protected]" },
{ name: "David", age: 16, email: undefined }
]
const adultEmails = Iterable.filterMap(
users,
(user) =>
user.age >= 18 && user.email ? Result.succeed(user.email) : Result.failVoid
)
console.log(Array.from(adultEmails)) // ["[email protected]", "[email protected]"]
// Use index in transformation
const items = ["a", "b", "c", "d", "e"]
const evenIndexItems = Iterable.filterMap(
items,
(item, i) => i % 2 === 0 ? Result.succeed(`${i}: ${item}`) : Result.failVoid
)
console.log(Array.from(evenIndexItems)) // ["0: a", "2: c", "4: e"]
filterMap: {
<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>): 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> => ({
[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 {
Iterator<B, any, any>.next(...[value]: [] | [any]): IteratorResult<B, any>next() {
let let result: IteratorResult<A, any>result = const iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
while (!let result: IteratorResult<A, any>result.done?: boolean | undefineddone) {
const const next: Result<B, X>next = f: (input: A, i: number) => Result<B, X>f(let 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 { IteratorYieldResult<TYield>.done?: false | undefineddone: false, IteratorYieldResult<B>.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 }
}
let result: IteratorResult<A, any>result = const iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
}
return { IteratorReturnResult<TReturn>.done: truedone: true, IteratorReturnResult<any>.value: anyvalue: var undefinedundefined }
}
}
}
})
)