Ramda.js R.set() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Ramda’s R.set() answers a write question: put this value at the piece a lens focuses, and give me a new structure. You pass a lens, then the new value, then the data. The lens writes the value. Native JavaScript’s cousin is a copy-and-assign: {...obj, x: 4} or nested spread. That is data-first and painful to reuse. set is data-last, so R.set(xLens, 4) is already a function of one object — ready for R.map or R.pipe.

The official sample is the whole idea. R.lensProp('x') focuses the x key. Setting 4 yields {x: 4, y: 2}. Setting 8 yields {x: 8, y: 2}. Need only to read? That is R.view. Need a function of the current value? That is R.over. Need one top-level key with no lens? That is R.assoc. Need a nested path with no lens? That is R.assocPath.

Syntax

Call R.set. Start with this line.

Ramda
R.set(lens, value, structure)
CallWhat it does
R.set(lens, value, structure)Primary signature.
R.set(...)Call the object helper.

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

Library declarations
Ramda
R.set(lens, value, structure)

Parameters

ParameterDescription
lensRequired. Lens. A focus built with R.lens, R.lensProp, R.lensIndex, or R.lensPath.
valueRequired. Any. The next focused leftover. Ramda does not look at the current value. Internally this is R.always(value) passed to over.
structureRequired. Any. The source object, array, or nested tree. It is not mutated when the lens setter is immutable.

Return Value

Return ValueDescription
Same shape as structureA new structure whose focused slot is value. Sibling keys and other indexes stay. Nested values off the path stay shared by reference.

Ramda Examples

📚 Getting Started

The official Ramda object sample, then a check that the source is not mutated.

Example 1 — Official xLens

Focus x. Write 4, then write 8. y stays 2.

Ramda
const xLens = R.lensProp('x');

R.set(xLens, 4, {x: 1, y: 2});
//=> {x: 4, y: 2}

R.set(xLens, 8, {x: 1, y: 2});
//=> {x: 8, y: 2}
Try It Yourself

How It Works

R.lensProp('x') is R.lens(R.prop('x'), R.assoc('x')). set never reads the current 1. It writes the value you pass. That is the docs sample from ramdajs.com.

Example 2 — No Mutation, Missing Key

The leftover is a new object. A missing key is created, the same way assoc would create it.

Ramda
const xLens = R.lensProp('x');
const orig = {x: 1, y: 2};
const copy = R.set(xLens, 4, orig);

copy;
//=> {x: 4, y: 2}

orig;
//=> {x: 1, y: 2}

copy === orig;
//=> false

R.set(xLens, 4, {});
//=> {x: 4}
Try It Yourself

How It Works

Built-in lenses use assoc. The source stays intact. Nested values off the path stay shared by reference. A missing key is not skipped the way modify skips it.

📈 Practical Patterns

Compare view, over, and assoc, then an index and a nested path, then a curried helper mapped over rows.

Example 3 — vs view, over, and assoc

Same xLens. view reads. set writes a literal. over maps a function. assoc matches set for one key.

Ramda
const xLens = R.lensProp('x');
const point = {x: 1, y: 2};

R.view(xLens, point);
//=> 1

R.set(xLens, 4, point);
//=> {x: 4, y: 2}

R.over(xLens, R.negate, point);
//=> {x: -1, y: 2}

R.assoc('x', 4, point);
//=> {x: 4, y: 2}
Try It Yourself

How It Works

set never looks at the current 1. over does. assoc agrees here because the key is top-level. Prefer assoc when you will never reuse a lens. Prefer set when the same focus must also support view and over, or when the slot is an index or a nested path.

Example 4 — lensIndex and Nested lensPath

Write the first array slot. Write a nested name. Sibling keys stay. The original tree is not mutated.

Ramda
R.set(R.lensIndex(0), 'FOO', ['foo', 'bar', 'baz']);
//=> ['FOO', 'bar', 'baz']

const nameLens = R.lensPath(['user', 'name']);
const orig = { user: { name: 'ada', city: 'Paris' } };

R.set(nameLens, 'Ada', orig);
//=> { user: { name: 'Ada', city: 'Paris' } }

orig;
//=> { user: { name: 'ada', city: 'Paris' } }
Try It Yourself

How It Works

R.lensIndex focuses one slot. R.lensPath clones along the path and copies sibling keys. R.lensProp('name') would miss, because name is not top-level. R.assocPath is the no-lens cousin of the nested leftover.

Example 5 — Curry and Map

Fill the lens and value to stamp many rows. The leftover is a function of one object.

Ramda
const markDone = R.set(R.lensProp('status'), 'done');

R.map(markDone, [
  { id: 1, status: 'open' },
  { id: 2, status: 'open' }
]);
//=> [{id: 1, status: 'done'}, {id: 2, status: 'done'}]
Try It Yourself

How It Works

Because set is arity 3, filling the lens and value leaves a function of one record. Native spread would need an arrow wrapper on every row. Need the next status to depend on the current one? Switch to R.over.

Browser Support

R.set() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.16.0+ · Any JS runtime

Ramda.js R.set()

Load Ramda 0.16.0 or later via CDN or npm, then call R.set with a lens, a value, and a structure. No extra browser API is required.

100% With Ramda 0.16.0+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
R.set() Universal

Bottom line: Safe in any project that includes Ramda 0.16.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].

Frequently Asked Questions

R.set(lens, value, structure) writes value at the focus of lens and returns a new structure. Added in v0.16.0. The original is not mutated when the lens setter is immutable. Internally it is R.over(lens, R.always(value), structure).
const xLens = R.lensProp('x'). R.set(xLens, 4, {x: 1, y: 2}) is {x: 4, y: 2}. R.set(xLens, 8, {x: 1, y: 2}) is {x: 8, y: 2}. y stays 2.
view only reads the focus. set replaces the focus with a value you already know. over reads the focus, runs a function, and writes the result. set(lens, v, s) is over(lens, always(v), s).
assoc writes one top-level key. assocPath writes a nested trail. set uses any lens: lensProp, lensIndex, lensPath, or a custom R.lens. For one key, set(lensProp('x'), 4, obj) matches assoc('x', 4, obj).
Built-in object lenses create the key, the same way assoc does. R.set(R.lensProp('x'), 4, {}) is {x: 4}. A missing nested path is created along the trail by lensPath.
No. R.set is a stable Ramda object helper from v0.16.0. It is not deprecated, experimental, or non-standard.

Continue to R.toPairs()

Learn how to turn an object into an array of key-value pairs using Ramda R.toPairs().

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