variable / @typed/fx

RefSubject.filterMap

Filters and transforms a RefSubject, Computed, or Filtered using a pure function that returns an Option.

Package version
2.0.0-beta.13
Category
Optional queries
Since
1.0.0
Member of
RefSubject

Import

import { RefSubject } from "@typed/fx";

Access RefSubject.filterMap through the imported RefSubject export. Its declaration below describes the member.

This public exposure is a re-export. Its import path is supported; declaration documentation is shared with the other public exposures below.

Signatures

export declare const filterMap: {
    <A, B>(f: (a: A) => Option.Option<B>): {
        <E, R>(ref: RefSubject<A, E, R> | Computed<A, E, R> | Filtered<A, E, R>): Filtered<B, E, R>;
        <R0, E0, B, E, R, E2, R2>(versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>, f: (a: A) => Option.Option<B>): Filtered<B, E0 | E | E2, R0 | R2>;
    };
    <R0, E0, A, E, R, B, E2, R2>(versioned: Versioned.Versioned<R0, E0, A, E, R, A, E2, R2>, f: (a: A) => Option.Option<B>): Filtered<B, E0 | E | E2, R0 | R2 | Exclude<R, Scope.Scope>>;
    <A, E, R, B>(ref: RefSubject<A, E, R> | Computed<A, E, R> | Filtered<A, E, R>, f: (a: A) => Option.Option<B>): Filtered<B, E, R>;
};

Why

Filters and transforms a RefSubject, Computed, or Filtered using a pure function that returns an Option. Current reads and pushed versions use the same derivation, preventing snapshot and subscription behavior from diverging.

Ownership and lifetime

The filter map view retains no independent value. Its Effect read fails with NoSuchElement while absent; the observing Scope owns and finalizes its Fx subscription.

Examples

import { Effect, Option } from "effect"
import * as RefSubject from "@typed/fx/RefSubject"

const program = Effect.gen(function* () {
  const numbers = yield* RefSubject.make([1, 2, 3, 4, 5])

  // Get the first even number
  const firstEven = RefSubject.filterMap(numbers, (arr) =>
    Option.fromNullable(arr.find((n) => n % 2 === 0))
  )

  const value = yield* firstEven
  console.log(value) // 2
})

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source