Functional JavaScript: Lenses

One of the most interesting talks among the ones I attended in lambda.world was Functional Lenses in JavaScript by FlavioCorpa. He talked about Functional Lenses in a practical way; what’s more, he started with his own small implementation (not for production), and then he talked about different libraries like Ramda or Monocle-TS.

The talk started with an easy-to-understand definition for those of us who are familiar with the procedural/imperative programming: “Lenses are basically functional getters and setters”. Basically, what we get with Lenses is the ability to reuse the data access of a structure in a modular and immutable way, either to obtain the data or to modify it. Later, we will see examples to better understand it.

Focus on Your Data Structures With Scala Lenses

With new programming techniques come new problems and new patterns to solve them.

In functional programming, immutability is a must. As a consequence, whenever it is needed to modify the content of a data structure, a new instance with updated values is created. Depending on how complex the data structure is, creating a copy may be a verbose task.