#👉What is Lens in Functional Programming? A lens is a composable pair of pure getter and setter functions which focus on a particular field
Explore tagged Tumblr posts
Video
youtube
👀👓🕶 👉What is Lens in Functional Programming? 👀👓🕶
👉What is Lens in Functional Programming?Â
 A lens is a composable pair of pure getter and setter functions which focus on a particular field inside an object, and obey a set of axioms known as the lens laws.Â
 👉Think of the object as the whole and the field as the part. The getter takes a whole and returns the part of the object that the lens is focused on.Â
👉The setter takes a whole, and a value to set the part to, and returns a new whole with the part updated. Unlike a function which simply sets a value into an object’s member field, Lens setters are pure functions:Â
 🤚🤚🤚Why Lens?Â
 👉Lenses allow you to abstract state shape behind getters and setters. Instead of littering your codebase with code that dives deep into the shape of a particular object, import a lens.Â
 👉If you later need to change the state shape, you can do so in the lens, and none of the code that depends on the lens will need to change.Â
 🤚🤚🤚Lens LawsÂ
 👉view(lens, set(lens, a, store)) ≡ a — If you set a value into the store, and immediately view the value through the lens, you get the value that was set.Â
👉set(lens, b, set(lens, a, store)) ≡ set(lens, b, store) — If you set a lens value to a and then immediately set the lens value to b, it's the same as if you'd just set the value to b.
 👉set(lens, view(lens, store), store) ≡ store — If you get the lens value from the store, and then immediately set that value back into the store, the value is unchanged.
0 notes