<Input, Pass, Fail>(
f: (input: Input) => Result.Result<Pass, Fail>
): Filter<Input, Pass, Fail>Creates a Filter from a function that returns either a pass or fail value.
Details
This is the primary constructor for creating custom filters. The function
should return either Result.succeed(value) or Result.fail(value).
Example (Creating custom filters)
import { Filter, Result } from "effect"
// Create a filter for positive numbers
const positiveFilter = Filter.make((n: number) => n > 0 ? Result.succeed(n) : Result.fail(n))
// Create a filter that transforms strings to uppercase
const uppercaseFilter = Filter.make((s: string) =>
s.length > 0 ? Result.succeed(s.toUpperCase()) : Result.fail(s)
)export const const make: <Input, Pass, Fail>(
f: (input: Input) => Result.Result<Pass, Fail>
) => Filter<Input, Pass, Fail>
Creates a Filter from a function that returns either a pass or fail value.
Details
This is the primary constructor for creating custom filters. The function
should return either Result.succeed(value) or Result.fail(value).
Example (Creating custom filters)
import { Filter, Result } from "effect"
// Create a filter for positive numbers
const positiveFilter = Filter.make((n: number) => n > 0 ? Result.succeed(n) : Result.fail(n))
// Create a filter that transforms strings to uppercase
const uppercaseFilter = Filter.make((s: string) =>
s.length > 0 ? Result.succeed(s.toUpperCase()) : Result.fail(s)
)
make = <function (type parameter) Input in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Input, function (type parameter) Pass in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Pass, function (type parameter) Fail in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Fail>(
f: (
input: Input
) => Result.Result<Pass, Fail>
f: (input: Inputinput: function (type parameter) Input in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Input) => import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.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) Pass in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Pass, function (type parameter) Fail in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Fail>
): interface Filter<in Input, out Pass = Input, out Fail = Input>Represents a filter function that can transform inputs to outputs or filter them out.
Details
A filter takes an input value and either returns a boxed pass value or the
special fail type to indicate the value should be filtered out.
Example (Defining a positive number filter)
import { Filter, Result } from "effect"
// A filter that only passes positive numbers
const positiveFilter: Filter.Filter<number> = (n) => n > 0 ? Result.succeed(n) : Result.fail(n)
console.log(positiveFilter(5)) // Result.succeed(5)
console.log(positiveFilter(-3)) // Result.fail(-3)
Filter<function (type parameter) Input in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Input, function (type parameter) Pass in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Pass, function (type parameter) Fail in <Input, Pass, Fail>(f: (input: Input) => Result.Result<Pass, Fail>): Filter<Input, Pass, Fail>Fail> => f: (
input: Input
) => Result.Result<Pass, Fail>
f as any