Ramda.js R.unfold() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.unfold(fn, seed) grows a list from one starting value. Your function looks at the current seed and answers one of two things: “add this value and keep going with this next seed”, or “stop” (official Ramda docs).

Stable since v0.10.0: R.unfold is not deprecated, experimental, or non-standard. Warning: your function must eventually return false — otherwise R.unfold loops forever and freezes the page.

Syntax

Call R.unfold. Start with this line.

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

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

Library declarations
Ramda
R.unfold(fn, seed) or R.unfold(fn)(seed)

Parameters

ParameterDescription
fnRequired. Receives one seed; returns [value, nextSeed] or false
seedOptional. The starting value, of any type; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array of every value, in the order they were produced

Ramda Examples

Example 1 — Official: Count by Ten

Push the negated seed, add 10 to the seed, and stop once it passes 50.

Ramda
const f = (n) => (n > 50 ? false : [-n, n + 10]);

console.log(R.unfold(f, 10));  // [-10, -20, -30, -40, -50]
Try It Yourself

How It Works

  1. Start. Ramda calls f(10). 10 is not above 50, so it returns [-10, 20].
  2. Push and move on. -10 goes into the list; 20 becomes the next seed.
  3. Repeat. Seeds 20, 30, 40 and 50 push -20 through -50.
  4. Stop. f(60) returns false, so the five values come back as the result.

Example 2 — Powers of Two up to 100

Use unfold when you know where to stop but not how many items you will get.

Ramda
const doubles = (n) => (n > 100 ? false : [n, n * 2]);

console.log(R.unfold(doubles, 1));    // [1, 2, 4, 8, 16, 32, 64]
console.log(R.unfold(doubles, 200));  // []
Try It Yourself

How It Works

  1. Value = seed. Each call pushes n and doubles it for the next call.
  2. Unknown count. You never say “7 items”; the list ends when the seed reaches 128, which is above 100.
  3. Immediate stop. Starting at 200 fails the check on the first call, so the result is [].
  4. Why not range? R.range only counts up by 1, so it cannot double.

Example 3 — Fibonacci Numbers with a Pair Seed

The seed can hold more than one number. Here it carries the last two Fibonacci numbers.

Ramda
const fib = ([a, b]) => (a > 50 ? false : [a, [b, a + b]]);

console.log(R.unfold(fib, [0, 1]));  // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Try It Yourself

How It Works

  1. Pair seed. The seed [0, 1] is unpacked into a = 0 and b = 1.
  2. Push one, shift both. It pushes a and continues with [b, a + b]: [1, 1], then [1, 2], then [2, 3].
  3. Stop. When a becomes 55, which is above 50, the function returns false.
  4. Key idea. The pushed value (a number) and the next seed (a pair) do not even need the same type.

Example 4 — Chunk a List into Pairs

Use the remaining list as the seed: take a chunk, continue with the rest.

Ramda
const chunk = (list) => (list.length === 0 ? false : [R.take(2, list), R.drop(2, list)]);

console.log(R.unfold(chunk, [1, 2, 3, 4, 5]));  // [[1, 2], [3, 4], [5]]
console.log(R.splitEvery(2, [1, 2, 3, 4, 5]));  // [[1, 2], [3, 4], [5]]
Try It Yourself

How It Works

  1. List seed. The seed starts as the whole list [1, 2, 3, 4, 5].
  2. Take and drop. R.take(2) gives the chunk to push; R.drop(2) gives the rest as the next seed.
  3. Stop. After [5] the rest is empty, so the function returns false.
  4. In real code. R.splitEvery does exactly this; unfold shows how it can be built.

Example 5 — Follow Parent Links to Build a Breadcrumb

Walk from a page up to the root, then reverse the path for display.

Ramda
const parents = { shoes: 'clothing', clothing: 'shop', shop: null };
const pathTo = R.unfold((key) => (key === null ? false : [key, parents[key]]));

console.log(pathTo('shoes'));                           // ['shoes', 'clothing', 'shop']
console.log(R.reverse(pathTo('shoes')).join(' > '));  // shop > clothing > shoes
Try It Yourself

How It Works

  1. Curry. Leaving out the seed makes pathTo a reusable one-argument function.
  2. Walk up. From 'shoes' it pushes the key and moves to its parent: 'clothing', then 'shop'.
  3. Stop at the root. parents.shop is null, so the next call returns false.
  4. Display. R.reverse puts the root first for a breadcrumb.

Browser & Runtime Support

R.unfold() is a few lines of plain ES5 JavaScript inside Ramda 0.10.0+. The examples on this page use arrow functions and parameter destructuring (ES2015), which every modern browser and Node.js 6+ support.

✓ Ramda 0.10.0+ · Any JS runtime

Ramda.js R.unfold()

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. No browser API is involved — it is just a while loop around your function.

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

Bottom line: Safe in any project with Ramda (check with npm ls ramda). Its behavior has not changed across versions. These labs load [email protected].

Frequently Asked Questions

R.unfold(fn, seed) builds a list from a single starting value. Ramda calls fn with the seed. If fn returns [value, nextSeed], the value is added to the list and fn is called again with nextSeed. If fn returns false, the list is finished. The official sample R.unfold(n => n > 50 ? false : [-n, n + 10], 10) returns [-10, -20, -30, -40, -50].
Return false from your function. Ramda stops on any falsy result (false, null, undefined, 0 or an empty string) and on an empty array, but false is the documented signal and the clearest to read. If your function never returns false, unfold loops forever.
In [value, nextSeed], value is what goes into the result list, and nextSeed is what your function receives on the next call. They can differ: the official sample pushes -n but continues with n + 10. The seed can be any type, such as a number, an array or an object.
Use R.range(from, to) for a simple run of whole numbers and R.times(fn, n) when you know how many items you need. Use R.unfold when the stop depends on the values themselves, such as powers of two up to 100, or when each step needs the previous state, such as Fibonacci numbers.
No. R.unfold is eager: it keeps calling your function until it returns false and builds the whole array in memory. For a lazy, possibly endless sequence, use a JavaScript generator function and take only the items you need.
No. R.unfold has been a stable Ramda list function since v0.10.0. It is not deprecated, experimental, or non-standard.

Next: R.uniq()

Remove duplicate items while keeping the first of each.

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