Hyperlinkv0.8.0-beta.28

Option

Option.firstSomeOfconsteffect/Option.ts:774
<T, C extends Iterable<Option<T>> = Iterable<Option<T>>>(collection: C): [
  C
] extends [Iterable<Option<infer A>>]
  ? Option<A>
  : never

Returns the first Some found in an iterable of Options, or None if all are None.

When to use

Use when you need the first available Some value from a priority list.

Details

  • Short-circuits on the first Some
  • Returns None only when every element is None

Example (Finding the first Some)

import { Option } from "effect"

console.log(Option.firstSomeOf([
  Option.none(),
  Option.some(1),
  Option.some(2)
]))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
error handlingorElse
Source effect/Option.ts:77411 lines
export const firstSomeOf = <T, C extends Iterable<Option<T>> = Iterable<Option<T>>>(
  collection: C
): [C] extends [Iterable<Option<infer A>>] ? Option<A> : never => {
  let out: Option<unknown> = none()
  for (out of collection) {
    if (isSome(out)) {
      return out as any
    }
  }
  return out as any
}