What is Lens in Functional Programming?

What is Lens in Functional Programming?

HomeFunctional ProgrammingWhat is Lens in Functional Programming?
What is Lens in Functional Programming?
ChannelPublish DateThumbnail & View CountDownload Video
Channel AvatarPublish Date not found Thumbnail
0 Views
What is Lens in Functional Programming?

A lens is a composable pair of pure getter and setter functions that focus on a particular field inside an object and obey a set of axioms called lens laws.
Consider the object as the whole and the field as the part. The getter takes a whole and returns the part of the object the lens is focused on.
The setter takes a whole and a value to set the part, and returns a new whole with the updated part. Unlike a function that simply sets a value in an object's member field, Lens setters are pure functions:

Why Lens?

Lenses allow you to abstract the shape of the state behind getters and setters. Instead of filling your codebase with code that dives deep into the shape of a particular object, import a lens.

If you later need to change the shape of the state, you can do so in the goal, and no code that depends on the goal will need to be changed.

Lentil Laws

view(lens, set(lens, a, store)) a — If you set a value in 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, then immediately set the lens value to b, this is the same as if you just set the value to b.
set(lens, view(lens, store), store) store — If you get the store lens value and then immediately set that value back to the store, the value remains unchanged.

Please take the opportunity to connect and share this video with your friends and family if you find it useful.