Ramda.js R.groupWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.groupWith(fn, list) splits a list into runs of neighbouring items. It calls fn(previous, current) for every adjacent pair: true keeps the pair in the same group, false starts a new one (official Ramda docs). Joining the groups back together gives the original list.

Stable since v0.21.0: R.groupWith is not deprecated, experimental, or non-standard. It compares neighbours only; to gather matching items from anywhere in a list, use R.groupBy.

Syntax

Call R.groupWith. Start with this line.

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

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

Library declarations
Ramda
R.groupWith(fn, list) or R.groupWith(fn)(list)

Parameters

ParameterDescription
fnRequired. Function. Gets two adjacent items (prev, curr); returns true to keep them together
listOptional. Array or String. The items to split into runs; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array of arrays; joined, they equal the input

Ramda Examples

Example 1 — Official Runs of Equal Numbers

Group neighbours that are equal — the first sample from the Ramda docs.

Ramda
const fib = [0, 1, 1, 2, 3, 5, 8, 13, 21];

console.log(JSON.stringify(R.groupWith(R.equals, fib)));
// [[0],[1,1],[2],[3],[5],[8],[13],[21]]
Try It Yourself

How It Works

  1. Pairwise test. R.equals already takes two values, so it is the test itself.
  2. Only one match. The pair 1, 1 is the only equal neighbours, so it forms [1, 1].
  3. Everything else splits. Every other pair differs, so each number gets its own group.
  4. Nothing lost. Joining the groups gives back the original fib list.

Example 2 — Turn Page Numbers into Ranges

Group consecutive numbers, then label each run like a print dialog: 1-3, 5-6, 9.

Ramda
const pages = [1, 2, 3, 5, 6, 9];

const runs = R.groupWith((a, b) => a + 1 === b, pages);

const label = (run) =>
  run.length === 1 ? String(run[0]) : R.head(run) + '-' + R.last(run);

console.log(JSON.stringify(runs));         // [[1,2,3],[5,6],[9]]
console.log(R.map(label, runs).join(', ')); // 1-3, 5-6, 9
Try It Yourself

How It Works

  1. Consecutive test. a + 1 === b is true when the next page directly follows the previous one.
  2. Runs. The gaps 3→5 and 6→9 fail the test, giving [[1, 2, 3], [5, 6], [9]].
  3. Label each run. R.head and R.last give the first and last page; a single page stays as-is.
  4. Join. R.map labels every run, and join(', ') builds the final text.

Example 3 — Official Vowel Runs in a String

Strings work too. Group neighbouring letters that are both vowels or both consonants.

Ramda
const isVowel = R.test(/^[aeiou]$/i);

console.log(JSON.stringify(R.groupWith(R.eqBy(isVowel), 'aestiou')));
// ["ae","st","iou"]
Try It Yourself

How It Works

  1. Vowel check. R.test builds isVowel, which returns true for one vowel letter.
  2. Compare results. R.eqBy(isVowel) asks: “Do these two letters give the same isVowel answer?”
  3. Runs. a e are vowels, s t are consonants, i o u are vowels.
  4. Strings back. Because the input is a string, each group is a string, not an array of letters.

Example 4 — Chat Bubbles from the Same Sender

Group back-to-back messages from one person, the way chat apps stack them into one bubble.

Ramda
const messages = [
  { from: 'Ana', text: 'Hi!' },
  { from: 'Ana', text: 'Are you free?' },
  { from: 'Ben', text: 'Yes' },
  { from: 'Ana', text: 'Great' }
];

const bubbles = R.groupWith(R.eqProps('from'), messages);

console.log(bubbles.length); // 3
bubbles.forEach((group) => {
  console.log(group[0].from + ': ' + R.pluck('text', group).join(' / '));
});
Try It Yourself

How It Works

  1. Same sender? R.eqProps('from') returns true when two messages have the same from.
  2. Three bubbles. Ana’s first two messages join; Ben breaks the run; Ana’s last message starts a new bubble.
  3. Order matters. Ana appears in two bubbles because her messages are not all next to each other.
  4. Print. R.pluck('text') collects the texts of each bubble.

Example 5 — groupWith vs groupBy

The same numbers, two very different answers: neighbours only vs the whole list.

Ramda
const nums = [1, 1, 2, 1];

console.log(JSON.stringify(R.groupWith(R.equals, nums))); // [[1,1],[2],[1]]
console.log(JSON.stringify(R.groupBy(String, nums)));     // {"1":[1,1,1],"2":[2]}
Try It Yourself

How It Works

  1. groupWith sees neighbours. The 2 sits between the 1s, so the last 1 starts its own group.
  2. groupBy sees everything. R.groupBy(String) files all three 1s under the key "1".
  3. Different shapes. groupWith returns an array; groupBy returns an object.
  4. Rule of thumb. Order and streaks matter → groupWith. Categories matter → groupBy.

Browser & Runtime Support

R.groupWith() is plain JavaScript inside Ramda 0.21.0+. It uses only loops and slice, so support depends only on loading Ramda.

✓ Ramda 0.21.0+ · Any JS runtime

Ramda.js R.groupWith()

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.21+ 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.groupWith() Universal

Bottom line: Safe in any project with Ramda 0.21.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.groupWith(fn, list) walks the list and compares each item with the one right before it. If fn(previous, current) returns true, the item joins the current group; if false, a new group starts. The result is an array of groups that, joined back together, equal the original list.
R.groupWith only looks at neighbours, so equal values separated by something else end up in different groups, and the result is an array. R.groupBy uses a key function, collects matching items from anywhere in the list, and returns an object.
No. fn always receives two adjacent items: the previous item and the current one. That is why a tolerance test like Math.abs(a - b) <= 2 can chain 1, 3, 5, 7 into one group even though 1 and 7 are far apart.
Yes. A string is treated as a list of characters, and each group comes back as a string. The official sample R.groupWith(R.eqBy(isVowel), 'aestiou') returns ['ae', 'st', 'iou'].
An empty array, []. An empty string also returns []. A null or undefined list throws a TypeError.
No. R.groupWith has been a stable Ramda List function since v0.21.0. It is not deprecated, experimental, or non-standard.

Next: R.head()

Get the first item of a list, or the first character of a string.

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