Lenses
Lenses
When dealing with an immutable record, you may want to update just one field and leave others alone.
Writing functions like change-size is tedious,
so Keen will automatically generate a lens for every record field.
set is a function that takes a record, a lens, and the new value.
In this case, it uses the size lens and sets the new size to 2.
If the new value depends on the old value, use modify.
modify works like set but takes a lambda to compute the new value from the old.
Combining lenses
a + b is a lens that gets / sets both a and b as a tuple.
a * b is a lens that delves into a then b.
Defining lenses
lens is an interface (see Interfaces and variants) and you can define your own.
The above example is simpler using computed-lens.
"then"
The then keyword provides a syntax for chaining several updates.
Each then must start on a new line.
It is followed by a function name, whose first argument is the previous line,
then the rest of the arguments (if any).
This is equivalent to:
then can be used with any function, but it's particularly suited to set and modify.
Using then if, the change can be conditional.