<A>(self: Iterable<A>): Option.Option<Array<A>>Returns all elements except the first safely, wrapped in an Option.
When to use
Use to safely get all elements after the first when the iterable may be empty.
Details
Allocates a new array via slice(1). Empty inputs return Option.none().
Example (Getting the tail)
import { Array } from "effect"
console.log(Array.tail([1, 2, 3, 4])) // Option.some([2, 3, 4])
console.log(Array.tail([])) // Option.none()export function function tail<A>(
self: Iterable<A>
): Option.Option<Array<A>>
Returns all elements except the first safely, wrapped in an Option.
When to use
Use to safely get all elements after the first when the iterable may be empty.
Details
Allocates a new array via slice(1). Empty inputs return Option.none().
Example (Getting the tail)
import { Array } from "effect"
console.log(Array.tail([1, 2, 3, 4])) // Option.some([2, 3, 4])
console.log(Array.tail([])) // Option.none()
tail<function (type parameter) A in tail<A>(self: Iterable<A>): Option.Option<Array<A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in tail<A>(self: Iterable<A>): Option.Option<Array<A>>A>): import OptionOption.type Option<A> = Option.None<A> | Option.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<interface Array<T>Array<function (type parameter) A in tail<A>(self: Iterable<A>): Option.Option<Array<A>>A>> {
const const as: A[]as = const fromIterable: <A>(
collection: Iterable<A>
) => A[]
Converts an Iterable to an Array.
When to use
Use to convert any Iterable (Set, Generator, etc.) into an array.
Details
If the input is already an array, this returns it by reference without
copying. Otherwise, it creates a new array from the iterable. Use copy if
you need a fresh array even when the input is already an array.
Example (Converting a Set to an array)
import { Array } from "effect"
const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result) // [1, 2, 3]
fromIterable(self: Iterable<A>self)
return const isReadonlyArrayNonEmpty: <A>(
self: ReadonlyArray<A>
) => self is NonEmptyReadonlyArray<A>
Checks whether a ReadonlyArray is non-empty, narrowing the type to
NonEmptyReadonlyArray.
When to use
Use when you need to prove a readonly array has at least one element without
requiring mutable array methods afterward.
Example (Checking for a non-empty readonly array)
import { Array } from "effect"
console.log(Array.isReadonlyArrayNonEmpty([])) // false
console.log(Array.isReadonlyArrayNonEmpty([1, 2, 3])) // true
isReadonlyArrayNonEmpty(const as: A[]as) ? import OptionOption.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 tailNonEmpty: <A>(
self: NonEmptyReadonlyArray<A>
) => Array<A>
Returns all elements except the first of a NonEmptyReadonlyArray.
When to use
Use to get all elements after the first when the array is known to be non-empty.
Example (Getting the tail of a non-empty array)
import { Array } from "effect"
console.log(Array.tailNonEmpty([1, 2, 3, 4])) // [2, 3, 4]
tailNonEmpty(const as: A[] & readonly [A, ...A[]]as)) : import OptionOption.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()
}