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)
Call
What 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
Parameter
Description
lens
Required. Lens. A focus built with R.lens, R.lensProp, R.lensIndex, or R.lensPath.
value
Required. Any. The next focused leftover. Ramda does not look at the current value. Internally this is R.always(value) passed to over.
structure
Required. Any. The source object, array, or nested tree. It is not mutated when the lens setter is immutable.
Return Value
Return Value
Description
Same shape as structure
A 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.
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.
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.
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.
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.
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.