Ramda.js R.scan() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.scan(fn, acc, list) is R.reduce that keeps every step. It folds the list from left to right, but instead of returning only the final answer, it returns the seed plus each running accumulator (official Ramda docs).

Stable since v0.10.0: R.scan is not deprecated, experimental, or non-standard. Its result always starts with the seed, so it is one item longer than the input.

Syntax

Call R.scan. Start with this line.

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

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

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

Parameters

ParameterDescription
fnRequired. Called as fn(acc, value); returns the next accumulator
accRequired. The starting value; always the first item of the result
listOptional. An array or string; omit it to get a reusable function

Return Value

Return ValueDescription
*An array of list.length + 1 accumulators

Ramda Examples

Example 1 — Official: Factorials

The sample from the Ramda docs: multiply as you go and keep every product.

Ramda
const numbers = [1, 2, 3, 4];
const factorials = R.scan(R.multiply, 1, numbers);

console.log(factorials);
// [1, 1, 2, 6, 24]
Try It Yourself

How It Works

  1. Seed. The result starts as [1].
  2. Multiply. R.multiply combines the running value with each number: 1, 2, 6, 24.
  3. Keep each step. Every new value is pushed onto the result.
  4. Result. Five values for four inputs; the last one, 24, is what R.reduce would return.

Example 2 — Running Bank Balance

Start with a balance and apply each transaction, keeping the balance after every one.

Ramda
const transactions = [-20, 50, -30];

console.log(R.scan(R.add, 100, transactions));
// [100, 80, 130, 100]
Try It Yourself

How It Works

  1. Opening balance. The seed 100 is the first entry.
  2. Withdraw. 100 + -20 = 80.
  3. Deposit, then withdraw. 80 + 50 = 130, then 130 + -30 = 100.
  4. Statement. The array is a full balance history, ready to show in a table or chart.

Example 3 — Running Maximum (High Score)

Track the best score so far after each game, and drop the seed with R.tail.

Ramda
const scores = [3, 1, 4, 1, 5];

console.log(R.tail(R.scan(R.max, 0, scores)));
// [3, 3, 4, 4, 5]
Try It Yourself

How It Works

  1. Compare. R.max keeps the larger of the best-so-far and the new score.
  2. Steps. From seed 0: 3, then 3 (1 is lower), 4, 4, 5.
  3. Drop the seed. R.tail removes the leading 0.
  4. One per game. The output now lines up with the five scores.

Example 4 — Keep a History of State

Return a new object at each step to get a clean snapshot of every state.

Ramda
const move = (pos, step) => R.assoc('x', pos.x + step, pos);
const history = R.scan(move, { x: 0 }, [2, 3, -1]);

console.log(R.pluck('x', history));
// [0, 2, 5, 4]
Try It Yourself

How It Works

  1. New object. R.assoc returns a copy with an updated x, so old states are never changed.
  2. Moves. Starting at { x: 0 }: +2 → 2, +3 → 5, −1 → 4.
  3. Snapshots. history holds four separate objects, one per state.
  4. Read them. R.pluck('x') lists the position at each step — handy for undo or replay.

Example 5 — A Reusable Running Total

Curry R.scan into a pipeline, and confirm the last value matches R.reduce.

Ramda
const runningTotal = R.pipe(R.scan(R.add, 0), R.tail);
console.log(runningTotal([1, 2, 3, 4]));  // [1, 3, 6, 10]

const nums = [1, 2, 3, 4];
console.log(R.last(R.scan(R.add, 0, nums)) === R.reduce(R.add, 0, nums));
// true
Try It Yourself

How It Works

  1. Curry. R.scan(R.add, 0) has no list yet, so it returns a function.
  2. Pipe. R.pipe runs the scan, then R.tail drops the seed: [1, 3, 6, 10].
  3. Compare. R.last of the scan is 10, and R.reduce also returns 10.
  4. Rule. The last scan value always equals the reduce result.

Browser & Runtime Support

R.scan() is plain JavaScript inside Ramda 0.10.0+. JavaScript has no built-in Array.prototype.scan, so without Ramda you would collect results by hand inside Array.prototype.reduce.

✓ Ramda 0.10.0+ · Any JS runtime

Ramda.js R.scan()

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.10+ 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.scan() Universal

Bottom line: Safe in any project with Ramda 0.10.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.scan(fn, acc, list) works like R.reduce, but instead of returning only the final result, it returns an array of every accumulator along the way, starting with the seed. The official sample R.scan(R.multiply, 1, [1, 2, 3, 4]) returns [1, 1, 2, 6, 24].
R.reduce returns one value: the final accumulator. R.scan returns a list of all accumulators, including the starting one. The last item of R.scan is always the same as the R.reduce result.
The seed comes first. For a list of n items, R.scan returns n + 1 values. Wrap it with R.tail to drop the seed: R.tail(R.scan(R.add, 0, [1, 2, 3])) returns [1, 3, 6].
Use R.scan(R.add, 0, list) for the totals with the starting 0, or R.pipe(R.scan(R.add, 0), R.tail) for one total per item. [1, 2, 3, 4] gives [1, 3, 6, 10].
No. R.scan does not understand R.reduced; the wrapper objects simply appear in the output. Trim the input first with R.takeWhile, or use R.reduceWhile if you only need the final value.
No. R.scan has been a stable Ramda list function since v0.10.0. It is not deprecated, experimental, or non-standard.

Next: R.sequence()

Turn a list of wrapped values inside out.

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