Ramda.js R.reduce() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.reduce(fn, acc, list) turns a whole list into one value. It walks the list from left to right and calls fn(acc, value) for each item; whatever fn returns becomes the next acc (official Ramda docs). This pattern is also called a fold.

Stable since v0.1.0: R.reduce is not deprecated, experimental, or non-standard. Note: unlike native Array.prototype.reduce, it does not skip holes in sparse arrays.

Syntax

Call R.reduce. Start with this line.

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

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

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

Parameters

ParameterDescription
fnRequired. Called as fn(acc, value); returns the next accumulator
accRequired. The starting value; also the result when the list is empty
listOptional. The array (or iterable) to walk; 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 main sample from the Ramda docs: start at 0 and subtract each number in turn.

Ramda
const result = R.reduce(R.subtract, 0, [1, 2, 3, 4]);

console.log(result);
// -10
Try It Yourself

How It Works

  1. Start. The accumulator begins at the seed 0.
  2. Each step. R.subtract(acc, value) computes acc - value: 0 - 1 = -1, -1 - 2 = -3, -3 - 3 = -6, -6 - 4 = -10.
  3. Carry forward. Every result becomes the acc for the next item.
  4. Result. After the last item, the final acc, -10, is returned.

Example 2 — Reusable Sum and Product

Leave out the list to build named helpers you can reuse anywhere.

Ramda
const sum = R.reduce(R.add, 0);
const product = R.reduce(R.multiply, 1);

console.log(sum([1, 2, 3, 4]));      // 10
console.log(product([1, 2, 3, 4]));  // 24
Try It Yourself

How It Works

  1. Curried. R.reduce(R.add, 0) has no list yet, so it returns a function that waits for one.
  2. Sum. R.add runs 0 + 1 + 2 + 3 + 4, giving 10.
  3. Product. R.multiply starts at 1, because a seed of 0 would make every product 0.
  4. Built-ins exist. Ramda already ships R.sum and R.product; this shows how they work.

Example 3 — Shopping Cart Total

Reduce a list of objects into a single number.

Ramda
const cart = [
  { name: 'Pen',  price: 2,  qty: 3 },
  { name: 'Book', price: 10, qty: 1 },
  { name: 'Bag',  price: 25, qty: 2 }
];

const addLine = (total, item) => total + item.price * item.qty;

console.log(R.reduce(addLine, 0, cart));
// 66
Try It Yourself

How It Works

  1. Seed. total starts at 0.
  2. Pen. 0 + 2 × 3 = 6.
  3. Book. 6 + 10 × 1 = 16.
  4. Bag. 16 + 25 × 2 = 66, the final total. The list holds objects, but the result is a number.

Example 4 — Count Words into an Object

The accumulator can be any type. Here it is an object of counts.

Ramda
const words = ['js', 'css', 'js', 'html', 'js'];

const tally = (counts, word) =>
  R.assoc(word, (counts[word] || 0) + 1, counts);

console.log(R.reduce(tally, {}, words));
// { js: 3, css: 1, html: 1 }
Try It Yourself

How It Works

  1. Seed. The accumulator starts as an empty object {}.
  2. Each word. counts[word] || 0 reads the current count (or 0 if the word is new) and adds 1.
  3. New object. R.assoc returns a copy with that key updated, so no object is mutated.
  4. Shortcut. For plain counting, R.countBy(R.identity, words) gives the same result.

Example 5 — Stop Early with R.reduced

Add numbers, but stop before the total goes past 10.

Ramda
const addUpTo10 = (acc, n) =>
  acc + n > 10 ? R.reduced(acc) : acc + n;

console.log(R.reduce(addUpTo10, 0, [1, 2, 3, 4]));  // 10
console.log(R.reduce(addUpTo10, 0, [2, 4, 6, 8]));  // 6
Try It Yourself

How It Works

  1. First list. 1 + 2 + 3 + 4 = 10 never goes past 10, so every item is added.
  2. Second list. 2 + 4 = 6; the next step 6 + 6 = 12 would pass 10.
  3. Stop. fn returns R.reduced(6), so Ramda stops and returns 6.
  4. Skipped. The last item, 8, is never visited. A plain return acc would not stop the loop.

Browser & Runtime Support

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

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.reduce()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so arrays reduce in Chrome, Firefox, Safari, and Edge. Via npm it works in Node.js, Deno, and Bun with require or import. Reducing other iterables such as Set needs an ES2015 runtime.

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.reduce() 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.reduce(fn, acc, list) walks the list from left to right and calls fn(acc, value) for each item. Whatever fn returns becomes the next acc, and the last acc is the result. The official sample R.reduce(R.subtract, 0, [1, 2, 3, 4]) returns -10.
fn never runs and you get the starting accumulator back. R.reduce(R.add, 0, []), R.reduce(R.add, 0, null), and R.reduce(R.add, 0, undefined) all return 0. Native [].reduce(fn) with no seed throws a TypeError instead.
R.reduce walks left to right and its iterator receives (acc, value). R.reduceRight walks right to left and its iterator receives (value, acc). With R.subtract on [1, 2, 3, 4] and seed 0, reduce gives -10 while reduceRight gives -2.
Return R.reduced(acc) from fn. Ramda stops walking and returns that value as the result. For example, a sum that refuses to pass 10 returns 6 for [2, 4, 6, 8] and never looks at 8.
R.reduce is curried and data-last, so R.reduce(R.add, 0) is a reusable sum function for R.pipe. Its fn receives only (acc, value), not the index or array. It returns the seed for null lists, supports R.reduced, and does not skip holes in sparse arrays.
No. R.reduce has been a stable Ramda list function since v0.1.0. It is not deprecated, experimental, or non-standard.

Next: R.reduceBy()

Group a list by key and fold each group to a single value.

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