effect-io-ai

Package: effect
Module: Option

Option.filter

Filters an Option using a predicate. Returns None if the predicate is not satisfied or the input is None.

When to use

Use when you need to discard an Option’s present value when it does not meet a condition, while narrowing the type via a refinement predicate.

Details

Example (Filtering with a predicate)

import { Option } from "effect"

const removeEmpty = (input: Option.Option<string>) =>
  Option.filter(input, (value) => value !== "")

console.log(removeEmpty(Option.some("hello")))
// Output: { _id: 'Option', _tag: 'Some', value: 'hello' }

console.log(removeEmpty(Option.some("")))
// Output: { _id: 'Option', _tag: 'None' }

console.log(removeEmpty(Option.none()))
// Output: { _id: 'Option', _tag: 'None' }

See

Signature

declare const filter: { <A, B extends A>(refinement: Refinement<A, B>): (self: Option<A>) => Option<B>; <A>(predicate: Predicate<A>): <B extends A>(self: Option<B>) => Option<B>; <A, B extends A>(self: Option<A>, refinement: Refinement<A, B>): Option<B>; <A>(self: Option<A>, predicate: Predicate<A>): Option<A>; }

Source

Since v2.0.0