effect-io-ai

Package: effect
Module: Option

Option.fromIterable

Converts an Iterable into an Option, wrapping the first element if it exists.

Details

This function takes an Iterable (e.g., an array, a generator, or any object implementing the Iterable interface) and returns an Option based on its content:

This utility is useful for safely handling collections that might be empty, ensuring you explicitly handle both cases where a value exists or doesn’t.

Example

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' }

Signature

declare const fromIterable: <A>(collection: Iterable<A>) => Option<A>

Source

Since v2.0.0