Ramda.js R.xprod() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.xprod(as, bs) returns a new list of pairs [a, b] — one for every combination of an item from as with an item from bs. In math this is the Cartesian product (official Ramda docs).

Stable since v0.1.0: R.xprod is not deprecated, experimental, or non-standard. The name is short for cross product: every item of one list paired with every item of another.

Syntax

Call R.xprod. Start with this line.

Ramda
R.xprod(as, bs) or R.xprod(as)(bs)
CallWhat it does
R.xprod(as, bs) or R.xprod(as)(bs)Primary signature.
R.xprod(...)Call the list helper.

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

Library declarations
Ramda
R.xprod(as, bs) or R.xprod(as)(bs)

Parameters

ParameterDescription
asRequired. First list; supplies the first item of every pair
bsOptional. Second list; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array of as.length × bs.length two-item arrays

Ramda Examples

Example 1 — Official: Every Pair of Two Lists

The sample from the Ramda docs: pair each number with each letter.

Ramda
console.log(R.xprod([1, 2], ['a', 'b']));
// [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
Try It Yourself

How It Works

  1. Take 1. Pair it with every letter: [1, 'a'], [1, 'b'].
  2. Take 2. Pair it with every letter: [2, 'a'], [2, 'b'].
  3. Collect. All pairs go into one flat list of two-item arrays.
  4. Size. 2 numbers × 2 letters = 4 pairs.

Example 2 — Build Product Variants

Combine every size with every color, then turn each pair into a SKU-style label.

Ramda
const sizes = ['S', 'M'];
const colors = ['red', 'blue'];

const variants = R.map(R.join('-'), R.xprod(sizes, colors));

console.log(variants);  // ['S-red', 'S-blue', 'M-red', 'M-blue']
Try It Yourself

How It Works

  1. Pairs. R.xprod(sizes, colors) gives [['S', 'red'], ['S', 'blue'], ['M', 'red'], ['M', 'blue']].
  2. Label. R.join('-') turns one pair into a string like 'S-red'.
  3. Every pair. R.map applies that join to all four pairs.
  4. Grouping. Sizes come first in each label because sizes is the first argument.

Example 3 — Result Size and Argument Order

Count the pairs, then swap the arguments to see how the pairs flip.

Ramda
console.log(R.xprod([1, 2, 3], ['a', 'b']).length);  // 6

console.log(R.xprod([1, 2], ['x']));  // [[1, 'x'], [2, 'x']]
console.log(R.xprod(['x'], [1, 2]));  // [['x', 1], ['x', 2]]
Try It Yourself

How It Works

  1. Multiply. 3 items × 2 items = 6 pairs, so .length is 6.
  2. First list first. In R.xprod([1, 2], ['x']), numbers come first in every pair.
  3. Swap. In R.xprod(['x'], [1, 2]), 'x' comes first instead.
  4. Same count. Swapping changes the pair order, never the number of pairs.

Example 4 — xprod vs zip

Both return pairs, but zip matches by position while xprod matches everything.

Ramda
const nums = [1, 2];
const letters = ['a', 'b'];

console.log(R.zip(nums, letters));    // [[1, 'a'], [2, 'b']]
console.log(R.xprod(nums, letters));  // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
Try It Yourself

How It Works

  1. zip. R.zip pairs index 0 with index 0, index 1 with index 1.
  2. zip size. It returns as many pairs as the shorter list: 2.
  3. xprod. R.xprod pairs every number with every letter.
  4. xprod size. It returns 2 × 2 = 4 pairs. Pick zip for matching rows, xprod for all combinations.

Example 5 — Grid Cells in a Pipeline

Leave out the second list to get a reusable function, then compose it.

Ramda
const withRows = R.xprod(['A', 'B']);
const toCells = R.pipe(withRows, R.map(R.join('')));

console.log(toCells([1, 2, 3]));  // ['A1', 'A2', 'A3', 'B1', 'B2', 'B3']
Try It Yourself

How It Works

  1. Curry. R.xprod(['A', 'B']) has no second list yet, so it returns a function.
  2. Pair. In the pipe, it receives [1, 2, 3] and builds 2 × 3 = 6 pairs like ['A', 1].
  3. Label. R.map(R.join('')) turns each pair into a cell name like 'A1'.
  4. Reuse. toCells works for any column list, such as [1, 2, 3, 4].

Browser & Runtime Support

R.xprod() is a plain nested loop inside Ramda 0.1.0+. Without Ramda, as.flatMap(a => bs.map(b => [a, b])) gives the same result, but flatMap (ES2019) needs Chrome 69+, Firefox 62+, Safari 12+, Edge 79+, or Node 11+.

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.xprod()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so it runs in Chrome, Firefox, Safari, and Edge, including older versions without flatMap. Via npm it works in Node.js, Deno, and Bun.

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

Bottom line: Safe in any project with Ramda (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.xprod(as, bs) returns a new list of every possible pair [a, b], taking a from as and b from bs. This is called the Cartesian product. The official sample R.xprod([1, 2], ['a', 'b']) returns [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']].
Exactly as.length × bs.length. Two items times three items gives six pairs. If either list is empty, the result is an empty array.
R.zip pairs items by position: R.zip([1, 2], ['a', 'b']) is [[1, 'a'], [2, 'b']]. R.xprod pairs every item with every item, so the same inputs give four pairs instead of two.
Use R.lift with a function of three arguments: R.lift((a, b, c) => [a, b, c])(as, bs, cs). It produces every combination as a three-item array, in the same order R.xprod would.
Pair the list with itself and keep only one order: R.filter(([a, b]) => a < b, R.xprod(teams, teams)). For ['Ana', 'Ben', 'Cy'] this gives [['Ana', 'Ben'], ['Ana', 'Cy'], ['Ben', 'Cy']].
No. R.xprod has been part of Ramda since v0.1.0 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.zip()

Pair two lists item by item, by position.

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