Hyperlinkv0.8.0-beta.28

Option

Option.productManyconsteffect/Option.ts:1672
<A>(self: Option<A>, collection: Iterable<Option<A>>): Option<
  [A, ...Array<A>]
>

Combines a primary Option with an iterable of Options into a tuple if all are Some.

When to use

Use when you need several Option values of the same type to all be Some and return them as a non-empty tuple.

Details

  • All SomeSome([self.value, ...rest])
  • Any NoneNone

Example (Combining many Options)

import { Option } from "effect"

const first = Option.some(1)
const rest = [Option.some(2), Option.some(3)]

console.log(Option.productMany(first, rest))
// Output: { _id: 'Option', _tag: 'Some', value: [1, 2, 3] }

console.log(Option.productMany(first, [Option.some(2), Option.none()]))
// Output: { _id: 'Option', _tag: 'None' }
combiningproductall
Source effect/Option.ts:167216 lines
export const productMany = <A>(
  self: Option<A>,
  collection: Iterable<Option<A>>
): Option<[A, ...Array<A>]> => {
  if (isNone(self)) {
    return none()
  }
  const out: [A, ...Array<A>] = [self.value]
  for (const o of collection) {
    if (isNone(o)) {
      return none()
    }
    out.push(o.value)
  }
  return some(out)
}