Ramda.js R.splitWhenever() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.splitWhenever(pred, list) splits a list at every item that passes a test. Those matching items act as separators: they end the current chunk and are left out (official Ramda docs). It never creates empty chunks, never changes the original, and R.splitWhenever(pred) is a reusable function.

Added in v0.28.0: R.splitWhenever is stable (not deprecated, experimental, or non-standard), but older Ramda builds do not include it — calling it there throws R.splitWhenever is not a function.

Syntax

Call R.splitWhenever. Start with this line.

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

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

Library declarations
Ramda
R.splitWhenever(pred, list) or R.splitWhenever(pred)(list)

Parameters

ParameterDescription
predRequired. Function. Called with one item; a truthy result marks a separator
listOptional. Array. The list to split; omit it to get a reusable function

Return Value

Return ValueDescription
*An array of chunks (arrays)

Ramda Examples

Example 1 — Split on Every 2 (Official Sample)

Every 2 is a separator: it cuts the list and disappears.

Ramda
const list = [1, 2, 3, 2, 4, 5, 2, 6, 7];

console.log(R.splitWhenever(R.equals(2), list)); // [[1], [3], [4, 5], [6, 7]]
console.log(list);                               // [1, 2, 3, 2, 4, 5, 2, 6, 7]
Try It Yourself

How It Works

  1. The test. R.equals(2) marks every 2 as a separator.
  2. Collect. Items that are not 2 go into the current chunk.
  3. Cut and drop. Each 2 closes the current chunk and is not kept anywhere.
  4. Nothing changed. Four new chunks come back; list still has all its 2s.

Example 2 — Group Lines into Paragraphs

Blank lines separate paragraphs — even when there are two in a row.

Ramda
const lines = ['Line A1', 'Line A2', '', 'Line B1', '', '', 'Line C1'];

console.log(R.splitWhenever(R.isEmpty, lines));
// [['Line A1', 'Line A2'], ['Line B1'], ['Line C1']]
Try It Yourself

How It Works

  1. The test. R.isEmpty returns true for the empty string ''.
  2. Cut at blanks. Every blank line ends the current paragraph and is dropped.
  3. No empty paragraph. The two blank lines in a row do not create an empty chunk between them.
  4. Result. Three paragraphs, each a list of its text lines.

Example 3 — Words from a String (No Empty Words)

Split on spaces, then join each character chunk back into a word.

Ramda
const text = 'hi  there you'; // note the double space

const toWords = R.pipe(
  R.splitWhenever(R.equals(' ')), // [['h', 'i'], ['t', 'h', ...], ...]
  R.map(R.join(''))               // ['hi', 'there', 'you']
);

console.log(toWords(text));   // ['hi', 'there', 'you']
console.log(text.split(' ')); // ['hi', '', 'there', 'you']
Try It Yourself

How It Works

  1. Characters are items. The string is read one character at a time; each space is a separator.
  2. Character chunks. The result is arrays of letters, such as ['h', 'i'].
  3. Join them. R.map with R.join('') turns each chunk into a word.
  4. Compare. Native split(' ') leaves an empty '' for the double space; splitWhenever does not.

Example 4 — Total Each Run Between Closed Days

A 0 means the shop was closed. Sum each stretch of open days.

Ramda
const sales = [5, 3, 0, 4, 0, 0, 6, 2];
const runs = R.splitWhenever(R.equals(0), sales);

console.log(runs);               // [[5, 3], [4], [6, 2]]
console.log(R.map(R.sum, runs)); // [8, 4, 8]
Try It Yourself

How It Works

  1. Split on closed days. Every 0 is a separator, so it is dropped.
  2. Two closed days in a row. The back-to-back zeros give no empty run.
  3. Sum each run. R.sum adds the numbers in every chunk: 5 + 3 = 8, 4, 6 + 2 = 8.
  4. Original intact. sales still holds all eight days, zeros included.

Example 5 — splitWhen vs splitWhenever

Same list, same test, very different results.

Ramda
const nums = [1, 0, 2, 3, 0, 4];
const isZero = R.equals(0);

console.log(R.splitWhen(isZero, nums));     // [[1], [0, 2, 3, 0, 4]]
console.log(R.splitWhenever(isZero, nums)); // [[1], [2, 3], [4]]
Try It Yourself

How It Works

  1. One cut. R.splitWhen stops at the first 0 and keeps it as the start of part 2.
  2. Every cut. R.splitWhenever cuts at both zeros.
  3. Drop vs keep. splitWhenever removes the zeros; splitWhen keeps every item.
  4. Pick by intent. “Where does it change?” → splitWhen. “Split on separators” → splitWhenever.

Browser & Runtime Support

R.splitWhenever() is plain JavaScript inside Ramda 0.28.0+. It is a simple for loop that pushes items into arrays — no modern browser APIs — so support depends only on loading a recent enough Ramda.

✓ Ramda 0.28.0+ · Any JS runtime

Ramda.js R.splitWhenever()

The UMD build (ramda.min.js) uses ES5 syntax, so it runs in every modern browser. Via npm it works in Node.js, Deno, and Bun with require or import.

100% With Ramda 0.28.0+
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 End of life · ES5 build may load, untested
Legacy only
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Node.js Supported · npm install ramda
Supported
Deno Supported · npm:ramda
Supported
Bun Supported · bun add ramda
Supported
R.splitWhenever() Universal

Bottom line: Safe in any project that loads Ramda 0.28.0 or later. Use ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. The Try-it labs load [email protected] and use modern syntax (const, arrow functions).

Frequently Asked Questions

An array of chunks. Every item that passes the test acts as a separator: it ends the current chunk and is left out. R.splitWhenever(R.equals(2), [1, 2, 3, 2, 4, 5, 2, 6, 7]) returns [[1], [3], [4, 5], [6, 7]].
No. Matching items are dropped, the way commas disappear when you split 'a,b,c'. If you need to keep the match, use R.splitWhen (one cut, match starts part 2) or R.groupWith.
No. Separators at the start, at the end, or next to each other never produce empty chunks. This differs from String.prototype.split: 'a,,b'.split(',') gives ['a', '', 'b'], while R.splitWhenever gives just the 'a' and 'b' chunks.
No match returns one chunk holding a copy of the list, such as [[1, 2, 3]]. If every item matches, or the list is empty, the result is [] — not [[]].
R.splitWhen cuts once at the first match and keeps the match. R.splitEvery cuts by length n and keeps every item. R.splitWhenever cuts at every match and removes the matches.
No. It is a stable Ramda List function, but it was added later than most: v0.28.0. Older Ramda builds do not have it, so calling it there throws 'R.splitWhenever is not a function'.

Next: R.startsWith()

Check whether a list or string begins with a given prefix.

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