Skip to main content
The Array module provides functional utilities for working with arrays in a type-safe and immutable way.

Type Definitions

type NonEmptyArray<A> = [A, ...Array<A>]
type NonEmptyReadonlyArray<A> = readonly [A, ...Array<A>]

Creating Arrays

import { Array } from "effect"

const result = Array.make(1, 2, 3)
console.log(result)
// [1, 2, 3]

Pattern Matching

match

import { Array } from "effect"

const match = Array.match({
  onEmpty: () => "empty",
  onNonEmpty: ([head, ...tail]) => `head: ${head}, tail: ${tail.length}`
})

console.log(match([]))
// "empty"

console.log(match([1, 2, 3]))
// "head: 1, tail: 2"

matchLeft

import { Array } from "effect"

const matchLeft = Array.matchLeft({
  onEmpty: () => "empty",
  onNonEmpty: (head, tail) => `head: ${head}, tail: ${tail.length}`
})

console.log(matchLeft([1, 2, 3]))
// "head: 1, tail: 2"

matchRight

import { Array } from "effect"

const matchRight = Array.matchRight({
  onEmpty: () => "empty",
  onNonEmpty: (init, last) => `init: ${init.length}, last: ${last}`
})

console.log(matchRight([1, 2, 3]))
// "init: 2, last: 3"

Transformations

map

import { Array } from "effect"

const result = Array.map([1, 2, 3], n => n * 2)
console.log(result)
// [2, 4, 6]

flatMap

import { Array } from "effect"

const result = Array.flatMap([1, 2, 3], n => [n, n * 2])
console.log(result)
// [1, 2, 2, 4, 3, 6]

filter

import { Array } from "effect"

const result = Array.filter([1, 2, 3, 4, 5], n => n > 2)
console.log(result)
// [3, 4, 5]

Concatenation

import { Array } from "effect"

const result = Array.prepend([2, 3, 4], 1)
console.log(result)
// [1, 2, 3, 4]

Accessing Elements

Folding

reduce

import { Array } from "effect"

const sum = Array.reduce([1, 2, 3, 4], 0, (acc, n) => acc + n)
console.log(sum)
// 10

reduceRight

import { Array } from "effect"

const result = Array.reduceRight(["a", "b", "c"], "", (acc, s) => acc + s)
console.log(result)
// "cba"

Searching

import { Array, Option } from "effect"

const result = Array.findFirst([1, 2, 3, 4], n => n > 2)
// Some(3)

Grouping & Partitioning

partition

import { Array } from "effect"

const [evens, odds] = Array.partition([1, 2, 3, 4, 5], n => n % 2 === 0)
console.log(evens)  // [2, 4]
console.log(odds)   // [1, 3, 5]

groupBy

import { Array } from "effect"

const grouped = Array.groupBy([1, 2, 3, 4, 5], n => n % 2 === 0 ? "even" : "odd")
console.log(grouped)
// { even: [2, 4], odd: [1, 3, 5] }

Sorting

import { Array, Order } from "effect"

const sorted = Array.sort([3, 1, 4, 1, 5], Order.number)
console.log(sorted)
// [1, 1, 3, 4, 5]

Deduplication

import { Array } from "effect"

const unique = Array.dedupe([1, 2, 2, 3, 3, 3])
console.log(unique)
// [1, 2, 3]

Conversions

import { Array } from "effect"

const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result)
// [1, 2, 3]
All Array operations are immutable and return new arrays without modifying the original.

Key Operations

OperationDescription
makeCreates an array from elements
makeByCreates an array by applying a function
mapTransforms each element
flatMapMaps and flattens results
filterFilters elements by predicate
reduceFolds array into single value
partitionSplits into two arrays by predicate
groupByGroups elements by key
sortSorts using an Order
dedupeRemoves duplicates

Build docs developers (and LLMs) love