Ramda.js R.mapAccum() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.mapAccum(fn, acc, list) is map and reduce in one pass. It walks the list left to right, carrying a running value (the accumulator), and returns [finalAcc, newList] (official Ramda docs). Use it when each new item depends on what came before it.

Stable since v0.10.0: R.mapAccum is a core Ramda list function. It is not deprecated, experimental, or non-standard.

Syntax

Call R.mapAccum. Start with this line.

Ramda
R.mapAccum(fn, acc, list) — curried, so R.mapAccum(fn, acc) waits for the list Callback (acc, item) => [nextAcc, newItem]
CallWhat it does
R.mapAccum(fn, acc, list) — curried, so R.mapAccum(fn, acc) waits for the list Callback (acc, item) => [nextAcc, newItem]Primary signature.
R.mapAccum(...)Call the list helper.

Prefer import { mapAccum } from "ramda" when you want a named import.

Library declarations
Ramda
R.mapAccum(fn, acc, list) — curried, so R.mapAccum(fn, acc) waits for the list Callback (acc, item) => [nextAcc, newItem]

Parameters

ParameterDescription
fnRequired. Called as fn(acc, item); must return [nextAcc, newItem]
accRequired. Starting accumulator (number, string, object…)
listRequired. Array to walk left to right — never changed

Return Value

Return ValueDescription
*[finalAcc, newList]

Ramda Examples

Example 1 — Official Sample: Growing Strings

Join digits one at a time and keep every in-between string.

Ramda
const digits = ['1', '2', '3', '4'];
const appender = (a, b) => [a + b, a + b];

console.log(R.mapAccum(appender, 0, digits));
// ['01234', ['01', '012', '0123', '01234']]
Try It Yourself

How It Works

  1. Pair out. appender returns [a + b, a + b] — the same value as the next accumulator and as the new item.
  2. Number + string. 0 + '1' is the text '01', so the accumulator becomes a growing string.
  3. Collect. Each step’s second value goes into the new list: '01', '012', …
  4. Final pair. The last accumulator '01234' comes first, then the list.

Example 2 — Running Bank Balance

Get the balance after each transaction and the final balance, then compare with R.scan.

Ramda
const transactions = [100, -30, 50, -20];
const step = (balance, t) => [balance + t, balance + t];

const [balance, history] = R.mapAccum(step, 0, transactions);

console.log(balance);                         // 100
console.log(history);                         // [100, 70, 120, 100]
console.log(R.scan(R.add, 0, transactions));  // [0, 100, 70, 120, 100]
Try It Yourself

How It Works

  1. Carry the balance. Each step adds the transaction: 0 → 100 → 70 → 120 → 100.
  2. Record it. The same new balance is stored as the mapped item, building history.
  3. Unpack. Destructuring splits the pair into balance (final) and history (one entry per transaction).
  4. vs scan. R.scan gives a similar list but starts with the initial 0 and has no separate final value.

Example 3 — Assign Sequential IDs

The accumulator is a counter; the new items are objects. They don’t have to be the same type.

Ramda
const names = ['Ann', 'Bob', 'Cy'];
const tag = (nextId, name) => [nextId + 1, { id: nextId, name }];

const [nextFree, users] = R.mapAccum(tag, 1, names);

console.log(nextFree); // 4
console.log(users);    // [{ id: 1, name: 'Ann' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Cy' }]
Try It Yourself

How It Works

  1. Counter in. The first call gets nextId = 1 and 'Ann'.
  2. Two different outputs. It returns 2 as the next counter and { id: 1, name: 'Ann' } as the new item.
  3. Repeat. Bob gets 2, Cy gets 3, and the counter ends at 4.
  4. Useful leftover. nextFree is the next unused ID — ready for the next batch.

Example 4 — Change from the Previous Item

Let the accumulator remember the last value so each item can be compared with it.

Ramda
const temps = [20, 23, 21, 26];
const change = (prev, t) => [t, t - prev];

console.log(R.mapAccum(change, temps[0], temps));
// [26, [0, 3, -2, 5]]
Try It Yourself

How It Works

  1. Start with the first reading. temps[0] (20) is the starting “previous” value, so the first change is 0.
  2. Pass the current value on. The step returns t as the next accumulator, so the next call sees it as prev.
  3. Store the difference. t - prev gives 3, -2, 5.
  4. Final value. The accumulator ends as the last reading, 26.

Example 5 — Keep Only One Half in a Pipe

Curry R.mapAccum with its first two arguments, then pick the part you need.

Ramda
const addUp = (a, x) => [a + x, a + x];

const runningTotals = R.pipe(R.mapAccum(addUp, 0), R.last);
const grandTotal    = R.pipe(R.mapAccum(addUp, 0), R.head);

console.log(runningTotals([1, 2, 3])); // [1, 3, 6]
console.log(grandTotal([1, 2, 3]));    // 6
Try It Yourself

How It Works

  1. Curry. R.mapAccum(addUp, 0) has no list yet, so it returns a function waiting for one.
  2. Run. R.pipe passes [1, 2, 3] in and gets [6, [1, 3, 6]].
  3. Pick the list. R.last returns the second item: [1, 3, 6].
  4. Pick the total. R.head returns the first item: 6.

Browser & Runtime Support

R.mapAccum() is plain JavaScript inside Ramda (available since v0.10.0). It uses no browser API, so support depends only on loading Ramda.

✓ Ramda 0.10.0+ · Any JS runtime

Ramda.js R.mapAccum()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so it runs in Chrome, Firefox, Safari, and Edge. Via npm it works in Node.js, Deno, and Bun with require or import. The arrow functions and destructuring in some examples need an ES2015+ engine, which every modern browser has.

100% With Ramda loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.mapAccum() Universal

Bottom line: Safe in any project that includes Ramda. Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. These labs load [email protected] from jsDelivr.

Frequently Asked Questions

R.mapAccum(fn, acc, list) walks the list from left to right, calling fn(acc, item) for each item. fn returns a pair [newAcc, newItem]. The result is [finalAcc, listOfNewItems] — a map and a reduce in one pass.
With appender = (a, b) => [a + b, a + b], R.mapAccum(appender, 0, ['1', '2', '3', '4']) returns ['01234', ['01', '012', '0123', '01234']].
R.scan(fn, acc, list) returns only the list of running accumulators, starting with the initial value: R.scan(R.add, 0, [1, 2]) is [0, 1, 3]. R.mapAccum lets each output item differ from the accumulator and also returns the final accumulator separately.
R.map transforms items but cannot remember anything between them. R.reduce remembers a value but returns only that one value. R.mapAccum does both: it carries a value and returns a new list.
A two-item array: [nextAccumulator, mappedItem]. Returning a single value breaks the result — the accumulator and every item become undefined.
No. R.mapAccum has been a stable Ramda list function since v0.10.0. It is not deprecated, experimental, or non-standard.

Next: R.mapAccumRight()

Carry a running value through a list from right to left.

R.mapAccumRight() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful