Hyperlinkv0.8.0-beta.28

Option

Option.filterMapconsteffect/Option.ts:1969
<A, B, X>(f: Filter.Filter<A, B, X>): (self: Option<A>) => Option<B>
<A, B, X>(self: Option<A>, f: Filter.Filter<A, B, X>): Option<B>

Transforms and filters an Option using a Filter callback.

When to use

Use to transform an Option's present value and discard it when the Filter fails.

Details

The callback returns a Result: Result.succeed keeps and transforms the value, while Result.fail discards it.

Example (Filtering and transforming)

import { Option, Result } from "effect"

console.log(Option.filterMap(
  Option.some(2),
  (n) => (n % 2 === 0 ? Result.succeed(`Even: ${n}`) : Result.failVoid)
))
// Output: { _id: 'Option', _tag: 'Some', value: 'Even: 2' }
filteringfilter
Source effect/Option.ts:196910 lines
export const filterMap: {
  <A, B, X>(f: Filter.Filter<A, B, X>): (self: Option<A>) => Option<B>
  <A, B, X>(self: Option<A>, f: Filter.Filter<A, B, X>): Option<B>
} = dual(2, <A, B, X>(self: Option<A>, f: Filter.Filter<A, B, X>): Option<B> => {
  if (isNone(self)) {
    return none()
  }
  const next = f(self.value)
  return result.isSuccess(next) ? some(next.success) : none()
})