Hyperlinkv0.8.0-beta.28

Option

Option.andThenconsteffect/Option.ts:1345
<A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>
<B>(f: Option<B>): <A>(self: Option<A>) => Option<B>
<A, B>(f: (a: A) => B): (self: Option<A>) => Option<B>
<B>(f: NotFunction<B>): <A>(self: Option<A>) => Option<B>
<A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>
<A, B>(self: Option<A>, f: Option<B>): Option<B>
<A, B>(self: Option<A>, f: (a: A) => B): Option<B>
<A, B>(self: Option<A>, f: NotFunction<B>): Option<B>

Chains a second computation onto an Option. The second value can be a plain value, an Option, or a function returning either.

When to use

Use when you need to chain an Option with a next step that may be another Option, a plain value, or a function.

Details

  • If self is None, returns None immediately
  • If f is a function, calls it with the Some value
  • If f returns an Option, returns it as-is; if a plain value, wraps in Some
  • If f is not a function, uses it directly (same wrapping rules)

Example (Chaining with andThen)

import { Option } from "effect"

// Chain with a function returning Option
console.log(Option.andThen(Option.some(5), (x) => Option.some(x * 2)))
// Output: { _id: 'Option', _tag: 'Some', value: 10 }

// Chain with a static value
console.log(Option.andThen(Option.some(5), "hello"))
// Output: { _id: 'Option', _tag: 'Some', value: "hello" }

// Chain with None - skips
console.log(Option.andThen(Option.none(), (x) => Option.some(x * 2)))
// Output: { _id: 'Option', _tag: 'None' }
sequencingflatMapmap
Source effect/Option.ts:134517 lines
export const andThen: {
  <A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>
  <B>(f: Option<B>): <A>(self: Option<A>) => Option<B>
  <A, B>(f: (a: A) => B): (self: Option<A>) => Option<B>
  <B>(f: NotFunction<B>): <A>(self: Option<A>) => Option<B>
  <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>
  <A, B>(self: Option<A>, f: Option<B>): Option<B>
  <A, B>(self: Option<A>, f: (a: A) => B): Option<B>
  <A, B>(self: Option<A>, f: NotFunction<B>): Option<B>
} = dual(
  2,
  <A, B>(self: Option<A>, f: (a: A) => Option<B> | Option<B>): Option<B> =>
    flatMap(self, (a) => {
      const b = isFunction(f) ? f(a) : f
      return isOption(b) ? b : some(b)
    })
)