Ramda.js R.mapAccumRight() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.mapAccumRight(fn, acc, list) is a map and a reduce in one, walking from the last item to the first. It carries a running value (acc) and builds a new item for every position (official Ramda docs). It returns [finalAcc, newList], and the new list keeps the original order.

Added in v0.10.0: R.mapAccumRight is stable — not deprecated, experimental, or non-standard. Warning: v0.26.0 changed the callback to (acc, value) returning [acc, value]; code written for the old (value, acc) order gives wrong results with no error.

Syntax

Call R.mapAccumRight. Start with this line.

Ramda
R.mapAccumRight(fn, acc, list) (curried, 3 arguments) Type ((acc, x) → [acc, y]) → acc → [x] → [acc, [y]]
CallWhat it does
R.mapAccumRight(fn, acc, list) (curried, 3 arguments) Type ((acc, x) → [acc, y]) → acc → [x] → [acc, [y]]Primary signature.
R.mapAccumRight(...)Call the list helper.

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

Library declarations
Ramda
R.mapAccumRight(fn, acc, list) (curried, 3 arguments) Type ((acc, x) → [acc, y]) → acc → [x] → [acc, [y]]

Parameters

ParameterDescription
fnRequired. Gets (acc, item); must return [newAcc, newItem]
accRequired. The starting running value, used with the last item
listOptional. An array (or string); omit it to get a reusable function

Return Value

Return ValueDescription
*The value returned by R.mapAccumRight().

Ramda Examples

Example 1 — Official Sample: Growing Strings

Join digits from the right onto a starting value of 5.

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

const [final, steps] = R.mapAccumRight(appender, 5, digits);
console.log(final);   // '12345'
console.log(steps);   // ['12345', '2345', '345', '45']
Try It Yourself

How It Works

  1. Last first. Ramda starts with '4' and acc = 5, giving '4' + 5 = '45'.
  2. Pair. appender returns the same string twice: once as the new acc, once as the item for that position.
  3. Grow left. Each step puts the next digit in front: '345', '2345', '12345'.
  4. Same order. '45' came from the last digit, so it sits at the end of steps.

Example 2 — Running Totals: Left vs Right

Use the same callback with R.mapAccum and R.mapAccumRight to see the direction change.

Ramda
const nums = [1, 2, 3, 4];
const runningTotal = (acc, x) => [acc + x, acc + x];

console.log(R.mapAccum(runningTotal, 0, nums));        // [10, [1, 3, 6, 10]]
console.log(R.mapAccumRight(runningTotal, 0, nums));   // [10, [10, 9, 7, 4]]
Try It Yourself

How It Works

  1. Callback. runningTotal adds the item to acc and returns the new total twice.
  2. Left. R.mapAccum totals 1, then 1+2, then 1+2+3…: each slot is the sum up to that item.
  3. Right. R.mapAccumRight totals 4, then 4+3, then 4+3+2…: each slot is the sum from that item to the end.
  4. Same final. Both final values are 10; only the per-item list differs.

Example 3 — Find Leaders (Bigger Than Everything to the Right)

Carry the biggest value seen so far from the right, and flag items that beat it.

Ramda
const nums = [16, 17, 4, 3, 5, 2];
const markLeader = (best, x) => [Math.max(best, x), x > best];

const [biggest, flags] = R.mapAccumRight(markLeader, -Infinity, nums);
console.log(biggest);                          // 17
console.log(flags);                            // [false, true, false, false, true, true]
console.log(nums.filter((_, i) => flags[i]));  // [17, 5, 2]
Try It Yourself

How It Works

  1. Start. -Infinity means “nothing seen yet”, so the last item 2 is always a leader.
  2. Flag. x > best is true only when the item beats every item to its right: 2, 5, and 17.
  3. Carry. Math.max(best, x) passes the biggest value so far to the next item on the left.
  4. Pick. The flags line up with nums, so filtering by index gives [17, 5, 2].

Example 4 — Build File Paths from the Right

Start from a file name and prefix each folder, collecting every partial path.

Ramda
const folders = ['home', 'ada', 'docs'];
const addFolder = (path, dir) => {
  const next = dir + '/' + path;
  return [next, next];
};

const [fullPath, paths] = R.mapAccumRight(addFolder, 'file.txt', folders);
console.log(fullPath);   // 'home/ada/docs/file.txt'
console.log(paths);      // ['home/ada/docs/file.txt', 'ada/docs/file.txt', 'docs/file.txt']
Try It Yourself

How It Works

  1. Start. The running value begins as 'file.txt'.
  2. Prefix. Walking right to left, 'docs', then 'ada', then 'home' are added in front with a /.
  3. Per folder. Each slot holds the path starting at that folder, so paths[1] is 'ada/docs/file.txt'.
  4. Final. The last running value is the full path 'home/ada/docs/file.txt'.

Example 5 — Add 1 to a Number Stored as Digits

Add the way you do on paper: start at the rightmost digit and carry to the left.

Ramda
const addDigit = (carry, d) => {
  const sum = d + carry;
  return [Math.floor(sum / 10), sum % 10];
};

const plusOne = digits => {
  const [carry, result] = R.mapAccumRight(addDigit, 1, digits);
  return carry ? R.prepend(carry, result) : result;
};

console.log(R.mapAccumRight(addDigit, 1, [1, 9, 9]));   // [0, [2, 0, 0]]
console.log(plusOne([1, 9, 9]));                        // [2, 0, 0]
console.log(plusOne([9, 9, 9]));                        // [1, 0, 0, 0]
Try It Yourself

How It Works

  1. Start carry. acc = 1 is the “+1” added to the rightmost digit.
  2. Each digit. 9 + 1 = 10 keeps digit 0 and carries 1; 1 + 1 = 2 carries 0.
  3. Result. The final acc is the carry left over: 0 for 199, 1 for 999.
  4. Overflow. When a carry remains, R.prepend adds it in front: 999 + 1 = [1, 0, 0, 0].

Browser & Runtime Support

R.mapAccumRight() has been in Ramda since v0.10.0; the current (acc, value) callback order dates from v0.26.0. It is a plain loop, so it works wherever Ramda loads.

✓ Ramda 0.26.0+ · Any JS runtime

Ramda.js R.mapAccumRight()

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.

100% With Ramda 0.26+
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.mapAccumRight() Universal

Bottom line: Safe in any project on Ramda 0.26 or newer; results are identical in 0.30 and 0.32. On older Ramda, swap the callback arguments. The destructuring in these examples needs an ES2015+ engine (all modern browsers and Node). These labs load [email protected].

Frequently Asked Questions

R.mapAccumRight(fn, acc, list) walks the list from the last item to the first, carrying a running value. For each item, fn(acc, item) returns [newAcc, newItem]. The result is [finalAcc, newList]. The official sample with (a, b) => [b + a, b + a], 5, and ['1', '2', '3', '4'] returns ['12345', ['12345', '2345', '345', '45']].
Both take the same callback and return [finalAcc, newList]. R.mapAccum walks left to right, so each output depends on the items before it. R.mapAccumRight walks right to left, so each output depends on the items after it. With (acc, x) => [acc + x, acc + x] on [1, 2, 3, 4], mapAccum gives [1, 3, 6, 10] and mapAccumRight gives [10, 9, 7, 4].
Ramda writes each new item back at the same index as the item it came from. Only the processing order is right to left; the positions in the returned list match the input.
Before v0.26.0, the callback received (value, acc) and returned [value, acc], and the whole result was [list, acc]. Since v0.26.0, the callback receives (acc, value) and returns [acc, value], and the result is [acc, list], matching R.mapAccum. Older tutorials using the old order give wrong results without an error.
Use R.reduceRight when you only need one final value. Use R.mapAccumRight when you also want a value for every position, such as a running total from the right or a carry digit for each column.
No. R.mapAccumRight has been part of Ramda since v0.10.0 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.mergeAll()

Merge a list of objects into a single object.

R.mergeAll() 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