You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lenses bundle the access to a specific property of a value for read and write operations.
Especially in programming languages which do not serve real mutability of values or objects, every little change of value requires the developer to make a copy.
Especially when working with bigger types, this especially leads developer to write lots of repetitive code.
Lenses try to reduce this overhead.
Example
Probably a lens in Lithia could be declared as follows:
data Lens {
get from
over changeValue, from
}
Let's image we have a person:
data Person {
name
age
}
And we want a function which ages the person by one year:
func age { person =>
Person person.name, person.age + 1
}
The bigger the data type grows, the bigger our function. With lenses we could reduce the overhead:
let age = Person.age { age => age + 1 }
// Person.age would be:
personAgeLens = Lens { p => p.age }, { changeAge, person =>
Person person.name, changeAge person.age
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
What are Lenses/Optics?
Lenses bundle the access to a specific property of a value for read and write operations.
Especially in programming languages which do not serve real mutability of values or objects, every little change of value requires the developer to make a copy.
Especially when working with bigger types, this especially leads developer to write lots of repetitive code.
Lenses try to reduce this overhead.
Example
Probably a lens in Lithia could be declared as follows:
Let's image we have a person:
And we want a function which ages the person by one year:
The bigger the data type grows, the bigger our function. With lenses we could reduce the overhead:
Benefits
Drawbacks
Beta Was this translation helpful? Give feedback.
All reactions