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
make
makeBy
range
replicate
import { Array } from "effect"
const result = Array.make(1, 2, 3)
console.log(result)
// [1, 2, 3]
import { Array } from "effect"
const result = Array.makeBy(5, n => n * 2)
console.log(result)
// [0, 2, 4, 6, 8]
import { Array } from "effect"
const result = Array.range(1, 5)
console.log(result)
// [1, 2, 3, 4, 5]
import { Array } from "effect"
const result = Array.replicate("a", 3)
console.log(result)
// ["a", "a", "a"]
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"
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
prepend
append
prependAll
import { Array } from "effect"
const result = Array.prepend([2, 3, 4], 1)
console.log(result)
// [1, 2, 3, 4]
import { Array } from "effect"
const result = Array.append([1, 2, 3], 4)
console.log(result)
// [1, 2, 3, 4]
import { Array } from "effect"
const result = Array.prependAll([3, 4], [1, 2])
console.log(result)
// [1, 2, 3, 4]
Accessing Elements
import { Array, Option } from "effect"
Array.head([1, 2, 3]) // Some(1)
Array.head([]) // None
import { Array, Option } from "effect"
Array.last([1, 2, 3]) // Some(3)
Array.last([]) // None
import { Array, Option } from "effect"
Array.get([1, 2, 3], 1) // Some(2)
Array.get([1, 2, 3], 5) // None
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
findFirst
findLast
contains
import { Array, Option } from "effect"
const result = Array.findFirst([1, 2, 3, 4], n => n > 2)
// Some(3)
import { Array, Option } from "effect"
const result = Array.findLast([1, 2, 3, 4], n => n > 2)
// Some(4)
import { Array } from "effect"
Array.contains([1, 2, 3], 2) // true
Array.contains([1, 2, 3], 5) // false
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
fromIterable
fromOption
fromRecord
import { Array } from "effect"
const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result)
// [1, 2, 3]
import { Array, Option } from "effect"
Array.fromOption(Option.some(1)) // [1]
Array.fromOption(Option.none()) // []
import { Array } from "effect"
const result = Array.fromRecord({ a: 1, b: 2, c: 3 })
console.log(result)
// [["a", 1], ["b", 2], ["c", 3]]
All Array operations are immutable and return new arrays without modifying the original.
Key Operations
| Operation | Description |
|---|
make | Creates an array from elements |
makeBy | Creates an array by applying a function |
map | Transforms each element |
flatMap | Maps and flattens results |
filter | Filters elements by predicate |
reduce | Folds array into single value |
partition | Splits into two arrays by predicate |
groupBy | Groups elements by key |
sort | Sorts using an Order |
dedupe | Removes duplicates |