List<A>

Immutable arrays with functional operations

Transform collections with map, filter, reduce without mutating the original data

What is List<A>?

List is an immutable collection that wraps arrays and provides functional operations. Every transformation creates a new List, preserving the original data.

const numbers = List([1, 2, 3, 4])

// Transform
const doubled = numbers.map((n) => n * 2) // List([2, 4, 6, 8])

// Filter
const evens = numbers.filter((n) => n % 2 === 0) // List([2, 4])

// Reduce
const sum = numbers.reduce((acc, n) => acc + n, 0) // 10

// Chain operations - take first 2 after transformations
const result = numbers
  .filter((n) => n > 1)
  .map((n) => n * 2)
  .toArray()
  .slice(0, 2) // [4, 6] - using array slice instead of take

// Convert back to array
const array = result // [4, 6] - result is already an array

Key Features

Immutable

All operations return new Lists - original data never changes

Optimized Performance

12x faster than nested flatMaps for complex operations

Functional Methods

map, filter, fold, groupBy, and 40+ more operations

Type-Safe

Full TypeScript inference for all transformations

Common Patterns

Creating Lists

const numbers = List([1, 2, 3, 4, 5])
const empty = List<number>([])
const range = List([...Array(5).keys()]) // List([0, 1, 2, 3, 4])

Transforming Data

const numbers = List([1, 2, 3, 4])
const result = numbers
  .filter((n) => n > 1)
  .map((n) => n * 2)
  .reduce((acc, n) => acc + n, 0) // 18

FlatMap Operations

const pairs = List([1, 2, 3]).flatMap((x) => List([x, x * 10]))
// List([1, 10, 2, 20, 3, 30])

When to Use List

Data transformations that need to preserve originals

UI state, user inputs, historical data

Functional pipelines with map/filter/fold

Data processing, analytics, transformations

Cartesian products with Do-notation

Generating combinations, test data, permutations