Ramda.js R.composeWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Performs right-to-left function composition using a transforming function (official Ramda docs). The last function may have any arity; the rest must be unary. Unlike compose, the functions are passed in an array.

Stable since v0.26.0: R.composeWith is a core Ramda helper — not deprecated, experimental, or non-standard. Unlike R.compose, functions are passed in an array, and the transformer is not used on the rightmost step.

Syntax

Call R.composeWith. Start with this line.

Ramda
R.composeWith(transformer, functions)
CallWhat it does
R.composeWith(transformer, functions)Primary signature.
R.composeWith(...)Call the function helper.

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

Library declarations
Ramda
R.composeWith(transformer, functions)

Parameters

ParameterDescription
transformerRequired. (f, res) → * — gate between steps
functionsRequired. Array; rightmost may be multi-arg; others unary

Return Value

Return ValueDescription
*One function (not auto-curried)

Ramda Examples

Example 1 — Official composeWhileNotNil

Increment age when it exists; skip when the property is missing.

Ramda
const composeWhileNotNil = R.composeWith((f, res) =>
  R.isNil(res) ? res : f(res)
);

console.log(composeWhileNotNil([R.inc, R.prop('age')])({ age: 1 }));
console.log(composeWhileNotNil([R.inc, R.prop('age')])({}));
Try It Yourself

How It Works

  1. Name the gate once. composeWhileNotNil wraps any array of functions with the same skip-nil rule.
  2. Rightmost first. R.prop('age') runs without the transformer.
  3. Age present. Value is 1 → transformer calls R.inc → 2.
  4. Age missing. Value is undefined → transformer returns it and never calls inc.

Example 2 — Always Apply the Next Function

A transformer that always returns f(res) matches plain compose — with an array.

Ramda
const composed = R.composeWith(
  (f, res) => f(res),
  [Math.abs, R.add(1), R.multiply(2)]
);

console.log(composed(-4)); // 7
Try It Yourself

How It Works

  1. Rightmost first. R.multiply(2)(-4) → -8 (no transformer).
  2. Next via xf. xf(R.add(1), -8) → -7.
  3. Finish via xf. xf(Math.abs, -7) → 7.
  4. No short-circuit. Always calling f(res) is compose with array syntax.

Example 3 — R.composeWith vs R.compose

Same three math steps — separate args vs array + always-apply transformer.

Ramda
const a = R.compose(Math.abs, R.add(1), R.multiply(2))(-4);

const b = R.composeWith(
  (f, res) => f(res),
  [Math.abs, R.add(1), R.multiply(2)]
)(-4);

console.log(a); // 7
console.log(b); // 7
Try It Yourself

How It Works

  1. Compose. Functions as separate arguments; every step always runs.
  2. ComposeWith. Functions in an array; transformer decides each later step.
  3. Same result here. Always-apply xf makes them match.
  4. When to switch. Use composeWith when you need skip-nil, skip-empty, unwrap, or logging between steps.

Example 4 — Later Steps Never Run

Two increments after prop('age'). A missing field never reaches either inc.

Ramda
const composeWhileNotNil = R.composeWith((f, res) =>
  R.isNil(res) ? res : f(res)
);

const pipeline = composeWhileNotNil([R.inc, R.inc, R.prop('age')]);

console.log(pipeline({ age: 1 })); // 3
console.log(pipeline({}));         // undefined
Try It Yourself

How It Works

  1. With age. 1 → first inc → 2 → second inc → 3.
  2. Without age. prop returns undefined.
  3. Gate stops the chain. Transformer returns undefined and never calls either inc.
  4. Reuse the helper. Same composeWhileNotNil works for longer arrays.

Example 5 — Stop on an Empty String

Trim first; skip toUpper when nothing is left.

Ramda
const untilEmpty = R.composeWith((f, res) =>
  res === '' ? res : f(res)
);

console.log(JSON.stringify(untilEmpty([R.toUpper, R.trim])('  hi  ')));
console.log(JSON.stringify(untilEmpty([R.toUpper, R.trim])('   ')));
Try It Yourself

How It Works

  1. Trim first (right). ' hi ' → 'hi'; spaces-only → ''.
  2. Non-empty continues. Transformer calls R.toUpper → 'HI'.
  3. Empty stops. Transformer returns '' and skips toUpper.
  4. Same pattern as nil. Change the sentinel; keep the gate shape.

Browser & Runtime Support

R.composeWith() is ordinary JavaScript once Ramda is loaded. It needs no special browser API — only a JS runtime that can run Ramda v0.26.0+.

✓ Ramda 0.26.0+ · Any JS runtime

Ramda.js R.composeWith()

Works in every modern browser and runtime where you can load Ramda 0.26.0 or later (CDN or npm). Older Ramda builds before 0.26.0 do not include composeWith.

100% With Ramda 0.26+
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.composeWith() Universal

Bottom line: Safe in any project that includes Ramda 0.26.0 or later. Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Bun, and Deno.

Frequently Asked Questions

R.composeWith(transformer, functions) composes an array of functions right to left, but each step except the last is wrapped by the transformer. The transformer receives (f, res) and decides whether to call f(res), skip, or replace the value.
const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res)); then composeWhileNotNil([R.inc, R.prop('age')])({age: 1}) yields 2, and the same pipeline on {} yields undefined. Added in v0.26.0. See also R.compose and R.pipeWith.
compose takes functions as separate arguments. composeWith takes a transformer first, then an array of functions. compose always calls every function. composeWith lets the transformer skip or change a step. The last (rightmost) function still runs without the transformer.
No. The docs note that the transforming function is not used on the last argument. The rightmost function in the array runs first, with the original input. Later steps (moving left) go through the transformer.
No. The result of composeWith is not automatically curried. Call it with every argument the rightmost function needs, in one go. composeWith itself is curried, so you can name a transformer once and reuse it on different arrays.
No. R.composeWith is a stable Ramda function helper from v0.26.0. It is not deprecated, experimental, or non-standard.

Next: R.construct()

Wrap a constructor so you can call it like a regular Ramda function.

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

11 people found this page helpful