3

My basic understanding of lensing is that, "a lens is a value representing maps between a complex type and one of its constituents. This map works both ways—we can get or "access" the constituent and set or "mutate" it"

I came across this when I was designing a machine learning library (neural nets), which demands keeping a big datastructure of parameters, groups of which need to be updated at different stages of the algorithm. I wanted to create the whole parameter data structure immutable, but changing a single group of parameters requires copying all the parameters, and recreating a new datastructure, which sounds inefficient. Not surprisingly other people have thought it too. Some people suggest using lensing, which in a sense, let you modify immutable datastructures. While some other people suggested just using mutables for these. Unfortunately I couldn't find anything on comparing these two paradigms, speed-wise, space-wise, code-complexity-wise etc.

Now question is, what are the pros/cons of using lensing vs mutable design?

Community
  • 1
  • 1
Daniel
  • 5,839
  • 9
  • 46
  • 85

1 Answers1

5

The trade offs between the two are pretty much as you surmised. Lenses are less complex than tracking the changes to a large immutable data structure manually, but still require more complex code than a mutable data structure, and there is some amount of runtime overhead. To know how much, you would have to measure, but it's probably less than you think, because a lot of the updated structure isn't copied but shared.

Mutable data structures are simpler and somewhat faster to modify, but harder to reason about, because now you have to take the order functions are called into account, worry about concurrency, and so forth.

Your third option is to make a bunch of small immutable data structures instead of one big one. Mutability often forces a single large data structure because of the need for a single source of truth, and to ensure that all references to data change at the same time. With immutability, this is a lot easier to control.

For example, you can have two separate Maps with the same type of key and different types of simple values, instead of one Map with a more complex value. Not only does this have performance benefits, it also makes it much easier to modularize your code.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
  • Thanks for the nice answer. One question though: why lensing does not create any issues for concurrency? (like if it is similar to mutables, it would make sense if it has the same problems with concurrency) – Daniel Feb 08 '17 at 21:29
  • 2
    Lensing is still immutable, it just creates an API that resembles a deep mutation. When you do a "set" anyone else who has a reference to any part of the old structure will still have the old unmodified value. You have to assign the result to a new variable in a new scope. – Karl Bielefeldt Feb 08 '17 at 22:41