Hyperlinkv0.8.0-beta.28

Option

Option.tapconsteffect/Option.ts:1597
<A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>
<A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>

Runs a side-effecting Option-returning function on the value of a Some, returning the original Option if the function returns Some, or None if it returns None.

When to use

Use to validate an Option's present value without transforming it, such as adding a side-condition check in a pipeline.

Details

  • NoneNone
  • Some → calls f(value); if result is Some, returns original self; if None, returns None

Example (Validating without transforming)

import { Option } from "effect"

const getInteger = (n: number) =>
  Number.isInteger(n) ? Option.some(n) : Option.none()

console.log(Option.tap(Option.some(1), getInteger))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }

console.log(Option.tap(Option.some(1.14), getInteger))
// Output: { _id: 'Option', _tag: 'None' }
sequencingflatMapfilter
Source effect/Option.ts:15974 lines
export const tap: {
  <A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>
  <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>
} = dual(2, <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A> => flatMap(self, (a) => map(f(a), () => a)))
Referenced by 1 symbols