Ramda.js R.traverse() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.traverse(of, fn, list) runs fn on every item, where fn returns a wrapped value, then flips the results into one wrapped list. In plain words: “try this on every item — give me all the results, or nothing if any try failed” (official Ramda docs).

Stable since v0.19.0: R.traverse is not deprecated, experimental, or non-standard. Note: since v0.29, R.of takes a type first — write R.of(Array), not R.of.

Syntax

Call R.traverse. Start with this line.

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

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

Library declarations
Ramda
R.traverse(of, fn, list) or R.traverse(of, fn)(list)

Parameters

ParameterDescription
ofRequired. Wraps a plain value; a function or a type with an of method
fnRequired. Called once per item; must return a wrapper of the same kind
traversableOptional. The list of plain values; omit it to get a reusable function

Return Value

Return ValueDescription
*One wrapper that holds the list of results (the input is not changed)

Ramda Examples

Example 1 — Official: Safe Division

Divide 10 by every number. Use [value] for success and [] for “cannot divide by zero”.

Ramda
const safeDiv = (n) => (d) => (d === 0 ? [] : [n / d]);

console.log(R.traverse(R.of(Array), safeDiv(10), [2, 4, 5]));  // [[5, 2.5, 2]]
console.log(R.traverse(R.of(Array), safeDiv(10), [2, 0, 5]));  // []
Try It Yourself

How It Works

  1. Wrap each answer. safeDiv(10)(2) gives [5]; safeDiv(10)(0) gives [].
  2. Map. The first list becomes [[5], [2.5], [2]]; the second becomes [[5], [], [2]].
  3. Flip. Three present values join into one list: [[5, 2.5, 2]].
  4. One failure. The empty [] leaves nothing to join, so the whole result is [].

Example 2 — Official: The Maybe Pattern

The docs use a Maybe type. Here is a tiny one, so the official samples run without a library.

Ramda
const Nothing = { map: () => Nothing, ap: () => Nothing, toString: () => 'Nothing' };
const Just = (x) => ({
  map: (f) => Just(f(x)),
  ap: (m) => m.map(x),
  toString: () => 'Just(' + JSON.stringify(x) + ')'
});
const Maybe = { of: Just };

const safeDiv = (n) => (d) => (d === 0 ? Nothing : Just(n / d));

console.log(String(R.traverse(Maybe, safeDiv(10), [2, 4, 5])));  // Just([5,2.5,2])
console.log(String(R.traverse(Maybe, safeDiv(10), [2, 0, 5])));  // Nothing
Try It Yourself

How It Works

  1. Maybe. Just(x) holds a value; Nothing holds none. Both have map and ap, which Ramda calls.
  2. Map. safeDiv(10) returns Just(5), Just(2.5), Just(2) — or Nothing for 0.
  3. Flip. Ramda starts from Maybe.of([]) and adds each value from the right, giving Just([5,2.5,2]).
  4. Any Nothing. Nothing.ap always returns Nothing, so one failed division makes the result Nothing.

Example 3 — Validate Every Input at Once

Turn form strings into numbers. Keep all of them only if every string is a valid number.

Ramda
const toNumber = (s) => (s.trim() !== '' && !isNaN(Number(s)) ? [Number(s)] : []);

console.log(R.traverse(R.of(Array), toNumber, ['1', '2', '3']));  // [[1, 2, 3]]
console.log(R.traverse(R.of(Array), toNumber, ['1', 'x', '3']));  // []
console.log(R.map(toNumber, ['1', 'x', '3']));                    // [[1], [], [3]]
Try It Yourself

How It Works

  1. Parse. toNumber('2') gives [2]; toNumber('x') gives [].
  2. All valid. Every string parses, so traverse returns one list of numbers: [[1, 2, 3]].
  3. One invalid. 'x' fails, so the whole batch is rejected: [].
  4. Compare with map. R.map keeps the hole in the middle; you would still have to check every item yourself.

Example 4 — Several Results per Item

If fn returns more than one value, traverse builds every combination — here, every upper/lower-case spelling.

Ramda
const cases = (c) => [c.toLowerCase(), c.toUpperCase()];

console.log(R.traverse(R.of(Array), cases, ['a', 'b']));
// [['a', 'b'], ['a', 'B'], ['A', 'b'], ['A', 'B']]
Try It Yourself

How It Works

  1. Map. cases turns ['a', 'b'] into [['a', 'A'], ['b', 'B']]: two choices per letter.
  2. Pick one of each. The flip pairs every choice for a with every choice for b.
  3. Count. 2 × 2 = 4 spellings. Three letters would give 2 × 2 × 2 = 8.
  4. Same rule as before. A one-item array is “one choice” and [] is “no choice” — that is why Example 1 works.

Example 5 — A Reusable Step in a Pipe

Leave out the list to get a reusable function, then drop it into R.pipe.

Ramda
const toNumber = (s) => (s.trim() !== '' && !isNaN(Number(s)) ? [Number(s)] : []);
const parseAll = R.traverse(R.of(Array), toNumber);
const total = R.pipe(R.split(','), parseAll, R.map(R.sum));

console.log(total('4,5,6'));  // [15]
console.log(total('4,x,6'));  // []
console.log(parseAll([]));    // [[]]
Try It Yourself

How It Works

  1. Curry. R.traverse(R.of(Array), toNumber) waits for the list, so parseAll is a one-argument function.
  2. Pipe. R.split gives ['4', '5', '6'], and parseAll gives [[4, 5, 6]].
  3. Work inside the wrapper. R.map(R.sum) sums the list inside: [15]. On failure it maps over [] and stays [].
  4. Empty input. No items means nothing can fail, so the result is success with an empty list: [[]].

Browser & Runtime Support

R.traverse() is plain JavaScript inside Ramda 0.19.0+. The R.of(Array) form shown here needs Ramda 0.29+; passing Array.of as the first argument also works in older versions.

✓ Ramda 0.19.0+ · Any JS runtime

Ramda.js R.traverse()

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. Custom wrapper types only need map and ap methods (or the fantasy-land/* versions); a custom list type can supply its own traverse.

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

Bottom line: Safe in any project with Ramda (check with npm ls ramda). If you upgrade from a version before 0.29, update every R.of passed to R.traverse. These labs load [email protected].

Frequently Asked Questions

R.traverse(of, fn, list) calls fn on every item, where fn returns a wrapped value such as [x], [] or a Maybe. It then flips the list of wrappers into one wrapper of a list. With safeDiv(10), the list [2, 4, 5] becomes [[5, 2.5, 2]], but [2, 0, 5] becomes [] because one division failed.
It tells Ramda how to wrap a plain value. Use R.of(Array), Array or Array.of for arrays, and a type with an of method (such as Maybe) for Maybe values. Ramda uses it to build the starting value, for example [[]] for arrays.
R.map(fn, list) only transforms each item, so you keep a list of wrappers like [[5], [], [2]]. R.sequence(of, list) flips wrappers that already exist. R.traverse(of, fn, list) does both in one call: it is the same as R.sequence(of, R.map(fn, list)).
[] means failure: at least one call returned an empty wrapper, so no result can be built. [[]] means success with no items: the input list was empty, so Ramda returns the starting value of([]).
No. Promises do not provide the of, map and ap methods Ramda needs, so R.traverse(Promise, fn, list) throws a TypeError. Use Promise.all(list.map(fn)) to run an async function on every item and wait for all results.
No. R.traverse has been a stable Ramda list function since v0.19.0. Since v0.29, R.of takes a type first, so pass R.of(Array) instead of R.of on its own.

Next: R.unfold()

Build a list step by step from a single seed value.

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