Ramda.js R.reduceWhile() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.reduceWhile(pred, fn, acc, list) is R.reduce with a stop rule. Before each step it asks pred(acc, value); while the answer is true, it folds with fn(acc, value), and the first false ends the loop and returns the current acc (official Ramda docs).

Stable since v0.22.0: R.reduceWhile is not deprecated, experimental, or non-standard. It stops at the first failing item — it does not skip it and keep going like a filter.

Syntax

Call R.reduceWhile. Start with this line.

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

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

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

Parameters

ParameterDescription
predRequired. Called as pred(acc, value) before each step; false stops the fold
fnRequired. Called as fn(acc, value); returns the next accumulator
accRequired. The starting value; returned for empty input or an instant stop
listOptional. An array or iterable; omit it to get a reusable function

Return Value

Return ValueDescription
*The accumulator at the moment the fold stopped or finished

Ramda Examples

Example 1 — Official Odd Sum

Both samples from the Ramda docs: stop at the first even number.

Ramda
const isOdd = (acc, x) => x % 2 !== 0;

console.log(R.reduceWhile(isOdd, R.add, 0, [1, 3, 5, 60, 777, 800]));  // 9
console.log(R.reduceWhile(isOdd, R.add, 111, [2, 4, 6]));               // 111
Try It Yourself

How It Works

  1. Check first. isOdd ignores acc and tests only the next number.
  2. First list. 1, 3, 5 pass and are added with R.add: 0 + 1 + 3 + 5 = 9.
  3. Stop. 60 is even, so the fold returns 9 without looking at 777 or 800.
  4. Second list. 2 fails immediately, so nothing is added and the seed 111 comes back.

Example 2 — Stay Within a Budget

Add prices in order, but never let the total go over 20.

Ramda
const prices = [5, 8, 4, 6, 2];

const fitsBudget = (total, price) => total + price <= 20;

console.log(R.reduceWhile(fitsBudget, R.add, 0, prices));
// 17
Try It Yourself

How It Works

  1. Uses acc. fitsBudget looks ahead: “would adding this price stay within 20?”
  2. Fits. 0 + 5 = 5, 5 + 8 = 13, 13 + 4 = 17 are all within 20.
  3. Too much. 17 + 6 = 23 is over 20, so the fold stops at 17.
  4. Not a filter. The last price, 2, would fit (19), but it is never checked. Use R.filter first if you want to skip items instead.

Example 3 — Collect Lines Before a Blank Line

The accumulator can be a list. Gather header lines until the first empty line.

Ramda
const lines = ['Title: Ramda', 'Author: Ada', '', 'Body text'];

const notBlank = (acc, line) => line !== '';
const addLine = (acc, line) => R.append(line, acc);

console.log(R.reduceWhile(notBlank, addLine, [], lines));
// ['Title: Ramda', 'Author: Ada']
Try It Yourself

How It Works

  1. Seed. The accumulator starts as an empty array [].
  2. Collect. The first two lines pass notBlank, and R.append adds each to a new array.
  3. Stop. The empty line '' fails, so 'Body text' is never reached.
  4. Simpler option. For plain collecting, R.takeWhile gives the same list; reduceWhile shines when you combine items into something else.

Example 4 — Check the Total Before Each Step

Keep adding while the running total is still under 10.

Ramda
const underTen = (acc) => acc < 10;

console.log(R.reduceWhile(underTen, R.add, 0, [4, 3, 5, 2, 7]));
// 12
Try It Yourself

How It Works

  1. Only acc. underTen uses just the running total and ignores the next item.
  2. Keep going. 0 < 10 → add 4; 4 < 10 → add 3; 7 < 10 → add 5, giving 12.
  3. Stop. Before adding 2, 12 < 10 is false, so the result is 12.
  4. Lesson. Checking the current total lets the result pass the limit once; check acc + value (Example 2) to never exceed it.

Example 5 — Reusable Curried Helper

Leave out the list to build a named function you can call on any data.

Ramda
const isPositive = (acc, n) => n > 0;
const sumLeadingPositives = R.reduceWhile(isPositive, R.add, 0);

console.log(sumLeadingPositives([3, 4, -1, 10]));  // 7
console.log(sumLeadingPositives([-2, 5]));         // 0
console.log(sumLeadingPositives([]));              // 0
Try It Yourself

How It Works

  1. Curried. Three of four arguments are given, so sumLeadingPositives waits for a list.
  2. First call. 3 + 4 = 7, then -1 fails, so 10 is ignored.
  3. Second call. -2 fails right away, so the seed 0 is returned.
  4. Third call. An empty list also returns the seed 0.

Browser & Runtime Support

R.reduceWhile() is plain JavaScript inside Ramda 0.22.0+. It uses no native array method or browser API, so support depends only on loading Ramda.

✓ Ramda 0.22.0+ · Any JS runtime

Ramda.js R.reduceWhile()

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

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

Bottom line: Safe in any project with Ramda 0.22.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.reduceWhile(pred, fn, acc, list) works like R.reduce, but checks pred(acc, value) before every step. While pred returns true, fn(acc, value) runs as usual. The first time pred returns false, it stops and returns the current acc. The official sample sums [1, 3, 5, 60, 777, 800] while numbers are odd and returns 9.
No. pred runs before fn, so the failing item is never folded in, and nothing after it is checked. In the official sample, 60 fails, so 60, 777, and 800 are all skipped even though 777 is odd.
Both stop a fold early. R.reduceWhile puts the stop rule in a separate pred function. R.reduced is returned from inside the reducer of R.reduce. Ramda builds reduceWhile on the same idea: when pred fails, it returns R.reduced(acc).
The starting accumulator. R.reduceWhile(isOdd, R.add, 101, []) returns 101, and null or undefined lists also return the seed. If the very first item fails pred, the seed is returned too.
Use R.takeWhile when you only want the leading items as a list. Use R.reduceWhile when you want to combine those items into a different value, such as a sum, or when the stop rule depends on the running total.
No. R.reduceWhile has been a stable Ramda list function since v0.22.0. It is not deprecated, experimental, or non-standard.

Next: R.reject()

Remove every item that passes a test — the opposite of filter.

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