Ramda.js R.reduceRight() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.reduceRight(fn, acc, list) turns a list into one value, starting from the last item and moving left. Each step calls fn(value, acc), and the result becomes the next acc (official Ramda docs). This is called a right fold; it nests to the right, as in 1 - (2 - (3 - 4)).

Stable since v0.1.0: R.reduceRight is not deprecated, experimental, or non-standard. Watch out: its iterator is (value, acc) — the reverse of R.reduce and of native reduceRight.

Syntax

Call R.reduceRight. Start with this line.

Ramda
R.reduceRight(fn, acc, list) or R.reduceRight(fn, acc)(list)
CallWhat it does
R.reduceRight(fn, acc, list) or R.reduceRight(fn, acc)(list)Primary signature.
R.reduceRight(...)Call the list helper.

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

Library declarations
Ramda
R.reduceRight(fn, acc, list) or R.reduceRight(fn, acc)(list)

Parameters

ParameterDescription
fnRequired. Called as fn(value, acc); returns the next accumulator
accRequired. The starting value; also the result for an empty array
listOptional. An array (or array-like); omit it to get a reusable function

Return Value

Return ValueDescription
*The final accumulator; the input list is never changed

Ramda Examples

Example 1 — Official Subtract

The sample from the Ramda docs, next to R.reduce with the same arguments.

Ramda
const nums = [1, 2, 3, 4];

console.log(R.reduceRight(R.subtract, 0, nums));  // -2
console.log(R.reduce(R.subtract, 0, nums));       // -10
Try It Yourself

How It Works

  1. Right first. R.reduceRight starts with the last item, 4, and the seed 0.
  2. Value first. Each step is R.subtract(value, acc): 4 - 0 = 4, 3 - 4 = -1, 2 - (-1) = 3, 1 - 3 = -2.
  3. Left fold. R.reduce calls R.subtract(acc, value) from the front: 0 - 1 - 2 - 3 - 4 = -10.
  4. Lesson. For order-sensitive operations like subtraction, direction changes the answer.

Example 2 — Build a Nested Object from a Path

Start with the innermost value and wrap it one key at a time, from the inside out.

Ramda
const path = ['user', 'profile', 'name'];

console.log(R.reduceRight(R.objOf, 'Ada', path));
// { user: { profile: { name: 'Ada' } } }
Try It Yourself

How It Works

  1. Perfect fit. R.objOf(key, value) takes the key first, matching the (value, acc) order.
  2. Innermost. R.objOf('name', 'Ada') → { name: 'Ada' }.
  3. Wrap. 'profile' wraps that, then 'user' wraps the result.
  4. Why right? The last key must be the deepest, so it has to be handled first. For updating existing objects, use R.assocPath.

Example 3 — Run Functions Right to Left

Apply a list of functions starting with the last one — exactly how R.compose works.

Ramda
const addOne = (n) => n + 1;
const double = (n) => n * 2;
const steps = [addOne, double];

const applyFn = (fn, value) => fn(value);

console.log(R.reduceRight(applyFn, 3, steps));  // 7
console.log(R.compose(addOne, double)(3));      // 7
Try It Yourself

How It Works

  1. Items are functions. Each value is a function, and acc is the number so far.
  2. Last first. double(3) runs first and gives 6.
  3. Then left. addOne(6) gives 7.
  4. Same as compose. R.compose also runs right to left; R.pipe is the left-to-right version.

Example 4 — Wrap Text in HTML Tags

The last tag should be closest to the text, so fold from the right.

Ramda
const wrap = (tag, html) => '<' + tag + '>' + html + '</' + tag + '>';

console.log(R.reduceRight(wrap, 'Hi', ['div', 'p', 'b']));
// <div><p><b>Hi</b></p></div>
Try It Yourself

How It Works

  1. Seed. The accumulator starts as the text 'Hi'.
  2. Innermost tag. wrap('b', 'Hi') → <b>Hi</b>.
  3. Outward. 'p' wraps that, then 'div' wraps the whole thing.
  4. Compare. A left fold, R.reduce((html, tag) => wrap(tag, html), 'Hi', tags), puts b on the outside: <b><p><div>Hi</div></p></b>.

Example 5 — Stop Early with R.reduced

Collect letters from the right and stop once you have three (from the Ramda tests).

Ramda
const takeThree = (letter, acc) =>
  acc.length === 3 ? R.reduced(acc) : acc + letter;

console.log(R.reduceRight(takeThree, '', ['a', 'd', 'm', 'a', 'r']));
// 'ram'
Try It Yourself

How It Works

  1. From the end. 'r' → 'r', then 'a' → 'ra', then 'm' → 'ram'.
  2. Check. At the next 'a', acc.length is already 3.
  3. Stop. fn returns R.reduced('ram'), so Ramda stops and returns 'ram'.
  4. Skipped. The first 'a' and 'd' are never visited.

Browser & Runtime Support

R.reduceRight() is a plain while loop inside Ramda 0.1.0+. It does not call native Array.prototype.reduceRight, so support depends only on loading Ramda.

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.reduceRight()

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.1+ 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.reduceRight() Universal

Bottom line: Safe in any project with Ramda 0.1.0 or later (check with npm ls 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].

Frequently Asked Questions

R.reduceRight(fn, acc, list) walks the list from the last item to the first and calls fn(value, acc) for each one. Whatever fn returns becomes the next acc, and the final acc is the result. The official sample R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) returns -2.
Direction and argument order. R.reduce walks left to right and calls fn(acc, value). R.reduceRight walks right to left and calls fn(value, acc). With R.subtract, seed 0, and [1, 2, 3, 4], reduce gives -10 and reduceRight gives -2.
Both walk from the right, but the native callback receives (acc, value, index, array) while Ramda's receives (value, acc). Ramda is also curried and data-last, supports R.reduced, and does not skip holes in sparse arrays.
An empty array returns the seed, so R.reduceRight(R.add, 0, []) is 0. Unlike R.reduce, null and undefined throw a TypeError because reduceRight reads list.length. Guard with R.defaultTo([]) if the list may be missing.
Yes. Return R.reduced(acc) from fn and Ramda stops immediately. In the Ramda tests, walking ['a', 'd', 'm', 'a', 'r'] from the right and stopping at three letters returns 'ram'.
No. R.reduceRight has been a stable Ramda list function since v0.1.0. It is not deprecated, experimental, or non-standard.

Next: R.reduceWhile()

Fold a list only while a condition keeps passing.

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