Example 1 — Chunk a List (Official Sample)
Group seven numbers in threes.
const nums = [1, 2, 3, 4, 5, 6, 7];
console.log(R.splitEvery(3, nums)); // [[1, 2, 3], [4, 5, 6], [7]]
console.log(nums); // [1, 2, 3, 4, 5, 6, 7] How It Works
- First chunk. Ramda copies items 0–2:
[1, 2, 3]. - Next chunk. It moves forward by 3 and copies
[4, 5, 6]. - Leftover. Only
7remains, so the last chunk is the shorter[7]. - Nothing changed. Each chunk is a new array;
numsis untouched.
