lorgnette
A set of lenses
Lenses are helper objects to retrieve or update data in immutable collections.
Basics
Lens is an object which allows to access object properties. Any lens has to implement next three methods:
// returns extracted property // clones object and returns it with altered property // same as set but callback is called to get new value
Methods get
and update
return new object instead of modifying given one.
Maybe
Method get
returns a value wrapped in Maybe monad. Maybe instance has
following methods:
// unpacks value or returns `orElse` // also known as `bind`, `>>=` or `flatMap` // calls given callback for `Nothing` only
Some examples:
; // returns 'value'nothing // returns 'anotherValue' { return ;} nothing // returns Nothing // returns Just('value!')) // returns Nothing nothing // returns Just(42) // returns just('value')
Predefined lenses
prop
Lens prop
allows access to object properties by name.
; let age = lens; // create lens to access property age age // returns Nothing age // returns Just(42) age // returns {name: 'John', age: 24} age // returns {name: 'John', age: 43}
This lens can also be configured to return default value instead of Nothing
when property does not exist.
; let age = lens; // create lens to access property age age // returns Just(18) age // returns Just(42)
at
Lens at
allows access to array elements by given index.
; let second = lens; second // returns Nothing second // returns Just(2) second // returns [1, 7, 3, 4, 5] second // returns [1, 9, 3, 4, 5] // negative index is also acceptedlet last = lens; last // returns Just(5) last // returns [1, 2, 3, 4, 7]
first
Lens first
is similar to at(0)
but it prepends new value when set
or
update
is called.
; let first = lens; first // returns Nothing first // returns Just(1) first // returns [7, 1, 2, 3, 4, 5] first // returns ['undefined', 1, 9, 3, 4, 5]
last
Lens last
is similar to at(-1)
but it appends new value when set
or
update
is called.
; let last = lens; last // returns Nothing last // returns Just(5) last // returns [1, 2, 3, 4, 5, 7] last // returns [1, 9, 3, 4, 5, 'undefined']
firstOf/lastOf
Lenses firstOf
/ lastOf
allow access to array elements by predicate.
firstOf' looks for an element satisfying a predicate from the beginning of an array.
lastOf` does the same but searched backward.
; let second = lens; second // returns Nothing second // returns Just({id: 2}) second // returns [{id: 1}, 7, {id: 3}, {id: 4}, {id: 5}] second // returns [{id: 1}, 11, {id: 3}, {id: 4}, {id: 5}]
Chaining
Lenses can be chained so access to nested properties is possible.
; let lastCartItem = lens; let cart = items: 'potato' 'cheese' ; lastCartItem // returns Just('cheese')lastCartItem // returns { items: ['potato', 'cheese', 'carrot'] }
Multilens
Sometimes it is usefull to access multiple properties at once. Multilens allows to do this.
; let lastCartItem = lens;let totalCount = lens;let cartLens = ; let cart = total: 2 items: 'potato' 'cheese' ; cartLens // returns [Just('cheese'), Just(2)] cartLens// returns// {// total: 3,// items: ['potato', 'cheese', 'carrot']// } cartLens// returns// {// total: 3,// items: ['potato', 'cheese', 'carrot']// }