Hyperlinkv0.8.0-beta.28

Array

Array.getconsteffect/Array.ts:951
(index: number): <A>(self: ReadonlyArray<A>) => Option.Option<A>
<A>(self: ReadonlyArray<A>, index: number): Option.Option<A>

Reads an element at the given index safely, returning Option.some or Option.none if the index is out of bounds.

When to use

Use when you need to read an array element by index and handle an out-of-bounds index as Option.none.

Details

The index is floored to an integer. This never throws.

Example (Accessing indexes safely)

import { Array } from "effect"

console.log(Array.get([1, 2, 3], 1)) // Some(2)
console.log(Array.get([1, 2, 3], 10)) // None
Source effect/Array.ts:9517 lines
export const get: {
  (index: number): <A>(self: ReadonlyArray<A>) => Option.Option<A>
  <A>(self: ReadonlyArray<A>, index: number): Option.Option<A>
} = dual(2, <A>(self: ReadonlyArray<A>, index: number): Option.Option<A> => {
  const i = Math.floor(index)
  return isOutOfBounds(i, self) ? Option.none() : Option.some(self[i])
})
Referenced by 1 symbols