For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/array/scan.md.

scan

Like reduce but returns an array of all intermediate accumulator values.

The result array starts with the initial value and ends with the final accumulator, so its length is array.length + 1.

scan([1, 2, 3, 4], (acc, n) => acc + n, 0)
// [0, 1, 3, 6, 10]

Usage

scan<T, R>(array: readonly T[], fn: (accumulator: R, value: T, index: number) => R, initial: R)

Like reduce but returns an array of all intermediate accumulator values.

The result array starts with the initial value and ends with the final accumulator, so its length is array.length + 1.

import { scan } from 'massaman'
// or:  import { scan } from 'massaman/array'

scan([1, 2, 3, 4], (acc, n) => acc + n, 0)
// [0, 1, 3, 6, 10]

Parameters

arrayreadonly T[]required

the source array.

fn(accumulator: R, value: T, index: number) => Rrequired

the function to apply.

initialRrequired

the initial accumulator value.

Returns

an array containing the initial value followed by every intermediate accumulator.

R[]