Hyperlinkv0.8.0-beta.28

Option

Option.partitionMapconsteffect/Option.ts:1925
<A, B, C>(f: (a: A) => Result<C, B>): (
  self: Option<A>
) => [left: Option<B>, right: Option<C>]
<A, B, C>(self: Option<A>, f: (a: A) => Result<C, B>): [
  left: Option<B>,
  right: Option<C>
]

Splits an Option into two Options using a function that returns a Result.

When to use

Use when you need to split an optional value into "left" and "right" channels using a Result-returning function.

Details

  • None[None, None]
  • Some where f returns Err[Some(error), None]
  • Some where f returns Ok[None, Some(value)]

Example (Partitioning by Result)

import { Option, Result } from "effect"

const parseNumber = (s: string): Result.Result<number, string> => {
  const n = Number(s)
  return isNaN(n) ? Result.fail("Not a number") : Result.succeed(n)
}

console.log(Option.partitionMap(Option.some("42"), parseNumber))
// Output: [{ _id: 'Option', _tag: 'None' }, { _id: 'Option', _tag: 'Some', value: 42 }]

console.log(Option.partitionMap(Option.some("abc"), parseNumber))
// Output: [{ _id: 'Option', _tag: 'Some', value: 'Not a number' }, { _id: 'Option', _tag: 'None' }]

console.log(Option.partitionMap(Option.none(), parseNumber))
// Output: [{ _id: 'Option', _tag: 'None' }, { _id: 'Option', _tag: 'None' }]
filteringfilter
Source effect/Option.ts:192513 lines
export const partitionMap: {
  <A, B, C>(f: (a: A) => Result<C, B>): (self: Option<A>) => [left: Option<B>, right: Option<C>]
  <A, B, C>(self: Option<A>, f: (a: A) => Result<C, B>): [left: Option<B>, right: Option<C>]
} = dual(2, <A, B, C>(
  self: Option<A>,
  f: (a: A) => Result<C, B>
): [excluded: Option<B>, satisfying: Option<C>] => {
  if (isNone(self)) {
    return [none(), none()]
  }
  const e = f(self.value)
  return result.isFailure(e) ? [some(e.failure), none()] : [none(), some(e.success)]
})