Hyperlinkv0.8.0-beta.28

Option

Option.fromIterableconsteffect/Option.ts:513
<A>(collection: Iterable<A>): Option<A>

Wraps the first element of an Iterable in a Some, or returns None if the iterable is empty.

When to use

Use when you need to safely extract the head of a collection, including generators or lazy iterables.

Details

  • Only consumes the first element; does not iterate the rest
  • Returns None for empty iterables

Example (Getting the first element)

import { Option } from "effect"

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

console.log(Option.fromIterable([]))
// Output: { _id: 'Option', _tag: 'None' }
constructorstoArray
Source effect/Option.ts:5136 lines
export const fromIterable = <A>(collection: Iterable<A>): Option<A> => {
  for (const a of collection) {
    return some(a)
  }
  return none()
}