<A>(self: Iterable<A>): Option<A>Gets the first element of a Iterable safely, or None if the Iterable is empty.
Example (Getting the first element)
import { Iterable, Option } from "effect"
const numbers = [1, 2, 3]
console.log(Iterable.head(numbers)) // Option.some(1)
const empty = Iterable.empty<number>()
console.log(Iterable.head(empty)) // Option.none()
// Safe way to get first element
const firstEven = Iterable.head(
Iterable.filter([1, 3, 4, 5], (x) => x % 2 === 0)
)
console.log(firstEven) // Option.some(4)
// Use with Option methods
const doubled = Option.map(Iterable.head([5, 10, 15]), (x) => x * 2)
console.log(doubled) // Option.some(10)export const const head: <A>(
self: Iterable<A>
) => Option<A>
Gets the first element of a Iterable safely, or None if the Iterable is empty.
Example (Getting the first element)
import { Iterable, Option } from "effect"
const numbers = [1, 2, 3]
console.log(Iterable.head(numbers)) // Option.some(1)
const empty = Iterable.empty<number>()
console.log(Iterable.head(empty)) // Option.none()
// Safe way to get first element
const firstEven = Iterable.head(
Iterable.filter([1, 3, 4, 5], (x) => x % 2 === 0)
)
console.log(firstEven) // Option.some(4)
// Use with Option methods
const doubled = Option.map(Iterable.head([5, 10, 15]), (x) => x * 2)
console.log(doubled) // Option.some(10)
head = <function (type parameter) A in <A>(self: Iterable<A>): Option<A>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Option<A>A>): type Option<A> = O.None<A> | O.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A>(self: Iterable<A>): Option<A>A> => {
const const iterator: Iterator<A, any, any>iterator = self: Iterable<A>self[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]()
const const result: IteratorResult<A, any>result = const iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
return const result: IteratorResult<A, any>result.done?: boolean | undefineddone ? import OO.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none() : import OO.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(const result: IteratorYieldResult<A>result.IteratorYieldResult<A>.value: Avalue)
}