Skip to content

Commit 9d53306

Browse files
author
jethro larson
committed
Add definition for lens hemanth#166
1 parent bf99fc0 commit 9d53306

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

readme.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ __Table of Contents__
5252
* [Setoid](#setoid)
5353
* [Semigroup](#semigroup)
5454
* [Foldable](#foldable)
55+
* [Lens](#lens)
5556
* [Type Signatures](#type-signatures)
5657
* [Algebraic data type](#algebraic-data-type)
5758
* [Sum type](#sum-type)
@@ -729,6 +730,56 @@ const sum = (list) => list.reduce((acc, val) => acc + val, 0)
729730
sum([1, 2, 3]) // 6
730731
```
731732

733+
## Lens ##
734+
A lens is a structure (often an object or function) that pairs a getter and a non-mutating setter for some other data
735+
structure.
736+
737+
```js
738+
// Using [Ramda's lens](http://ramdajs.com/docs/#lens)
739+
const nameLens = R.lens(
740+
// getter for name property on an object
741+
(obj) => obj.name,
742+
// setter for name property
743+
(val, obj) => Object.assign({}, obj, {name: val})
744+
)
745+
```
746+
747+
Having the pair of get and set for a given data structure enables a few key features.
748+
749+
```js
750+
const person = {name: 'Gertrude Blanch'}
751+
752+
// invoke the getter
753+
R.view(nameLens, person) // 'Gertrude Blanch'
754+
755+
// invoke the setter
756+
R.set(nameLens, 'Shafi Goldwasser', person) // {name: 'Shafi Goldwasser'}
757+
758+
// run a function on the value in the structure
759+
R.over(nameLens, uppercase, person) // {name: 'GERTRUDE BLANCH'}
760+
```
761+
762+
Lenses are also composable. This allows easy immutable updates to deeply nested data.
763+
764+
```js
765+
// This lens focuses on the first item in a non-empty array
766+
const firstLens = R.lens(
767+
// get first item in array
768+
xs => xs[0],
769+
// non-mutating setter for first item in array
770+
(val, [__, ...xs]) => [val, ...xs]
771+
)
772+
773+
const people = [{name: 'Gertrude Blanch'}, {name: 'Shafi Goldwasser'}]
774+
775+
// Despite what you may assume, lenses compose left-to-right.
776+
R.over(compose(firstLens, nameLens), uppercase, people) // [{'name': 'GERTRUDE BLANCH'}, {'name': 'Shafi Goldwasser'}]
777+
```
778+
779+
Other implementations:
780+
* [partial.lenses](https://github.com/calmm-js/partial.lenses) - Tasty syntax sugar and a lot of powerful features
781+
* [nanoscope](http://www.kovach.me/nanoscope/) - Fluent-interface
782+
732783
## Type Signatures
733784

734785
Often functions in JavaScript will include comments that indicate the types of their arguments and return values.

0 commit comments

Comments
 (0)