---
title: Std.Array
---

## Types

### Array

```saga
opaque type Array a
```

A persistent, immutable indexed collection backed by Erlang's `array` module.
Provides O(log n) random access by index — much faster than linked lists
for index-heavy workloads, but slower than lists for sequential processing.

## Functions

### new

```saga
fun new : Unit -> Array a
```

Creates a new empty array.

### from_list

```saga
fun from_list : (xs: List a) -> Array a
```

Creates an array from a list.

### size

```saga
fun size : (arr: Array a) -> Int
```

Returns the number of elements in the array.

### get

```saga
fun get : (index: Int) -> (arr: Array a) -> Maybe a
```

Gets the element at the given index. Returns Nothing if out of bounds.

### set

```saga
fun set : (index: Int) -> (value: a) -> (arr: Array a) -> Array a
```

Sets the element at the given index, returning a new array.
If the index is beyond the current size, the array is extended with a default value.

### to_list

```saga
fun to_list : (arr: Array a) -> List a
```

Converts an array to a list.

### map

```saga
fun map : (f: a -> b) -> (arr: Array a) -> Array b
```

Applies a function to every element, returning a new array.

### foldl

```saga
fun foldl : (f: b -> a -> b) -> (acc: b) -> (arr: Array a) -> b
```

Left fold over the array elements, in index order.

