Yes, using Monocle for years now. So happy to have rich ecosystem of libraries in Scala land.
solid_fuel 13 hours ago [-]
I haven't encountered this pattern before. Is there some more information on what problems this is designed to solve?
kqr 2 hours ago [-]
Aside from making it convenient to build getters and setters for nested data, optics also have conditional accessors and collection accessors as building blocks.
Conditional accessors have since become a popular language feature with e.g. the elvis operator ?. in C#. Optics make it possible to innovate on features like that in library code rather than as language features.
Something I've yet to see made into a language feature that is common in optics libraries are iterating accessors. E.g. to reset all counters we can, with optics, say something like (in Haskell syntax, since that is what I know)
stats.each.count .= 0
and that sets the count to zero for all objects in the stats array. If not all stats objects have a count (some might be of a gauge type, for example) we can compose in a conditional accessor that resolves the count field only if it is not null:
stats.each.count._Just .= 0
In the above statement, nothing is language syntax --it's all library functions and operators. But it still combines really well on the page. Once one knows optics, one rarely has to think very hard about how to do any get or update operation, even when the data types become complicated.
dkarl 11 hours ago [-]
The problem most programmers would be familiar with is making an update inside a deeply nested immutable data structure. For example, suppose you want to update a user's billing address, and you have an immutable data structure that looks like this:
user { billingInfo: { card, address }, name, subscription { level, expiration }, status { level, since } }
The structure is immutable, so you can't make the update in place. On the other hand, you're only changing one field, so it would be wasteful to make a complete deep copy. The efficient way to create an updated instance is to create a new user instance and a new billingInfo instance while reusing the name, subscription, and status instances.
You can think of this as the equivalent of a setter for immutable data structures.
This is an artificial example, because the cost of making a deep copy of this user structure is probably not that bad, and the boilerplate to make an efficient update is not all that bad, either. You would use an optics library when 1) you need efficient updates and 2) it's worth investing a little effort to hide the boilerplate.
Optics also let you concisely express access into a deeply nested structure, the getter paired with the setter. In my experience, updates are the motivation for setting up optics, and concise access is a nice thing you get as a bonus.
usrusr 9 hours ago [-]
Does it offer creating a mutable view on top of the immutable structure that can be used to accumulate a whole set of changes to later be materialized into a copy with the changes? (or to be used directly, at whatever cost would be required) That's something I've been wondering why it's not more of an established thing. It would basically be docker, but for in-memory reference graphs instead of filesystems.
You can compose together a bunch of edit operations (an edit command, if you like) and apply them at once at the end, is that what you mean?
Nullabillity 12 hours ago [-]
If monads are programmable semicolons (ways to chain operation), lenses are programmable dots (ways to delegate access to data). Other optics are largely generalizations of that pattern.
AlotOfReading 12 hours ago [-]
Lens are the functional version of getters and setters. A lens takes a product type (struct, class, etc) and allows you to view or update part of it. Prisms are something similar for sum types (variants) that allow you to look at a value if it's present and err otherwise.
The optical analogy comes from how these operations resemble zooming in on structures with a magnifying glass and the entire family of related transformations is called optics.
jeremyjh 11 hours ago [-]
Lenses make it easier to read and update members deep in a hierarchy of read-only data structures.
mhitza 9 hours ago [-]
Does the LSP provide clear autocomplete on what properties can be accessed on the bound _ ?
Asking as someone that doesn't use Scala at all, but has seen the hit-and-miss of some FP language LSPs.
valenterry 6 hours ago [-]
Yes it does, otherwise the code would actually not compile.
lmm 6 hours ago [-]
IntelliJ or the older Scala-IDE for Eclipse certainly does, so I'd be very disappointed if the LSP impl (which the Scala maintainers have been pushing as the official IDE replacement these days) didn't.
> In computer science, optics are a general class of bidirectional transformations
signaru 8 hours ago [-]
The name, monocle, also further misleads those expecting the physics topic. They actually have a nice logo with a lens and the lambda symbol which is often the symbol used for wavelength.
dkarl 5 hours ago [-]
Virtually everything in computer science is a metaphor. A computer was a human being before it was a machine. A block of memory, an array of values, an index into a structure, most of the vocabulary we use every day is built out of metaphors.
This is why I read the comments before I click on the link. :)
evertedsphere 8 hours ago [-]
"this has nothing to do with classes, which in sociology and related fields are strata which society can be analysed as being divided into"
Xophmeister 10 hours ago [-]
It’s a metaphor.
dpratt 5 hours ago [-]
Imagine my disappointment when I spent the time to set up a Cassandra instance and it did not immediately materialize a demigod woman who knew the answers to everything but was cursed to have no one believe her.
Every scala code base I have worked on, that wasnt written by small team of experts, turned into a huge pile of crap. A small squad of people that treat the language like a religion create an impenetrable masterpiece
threeseed 11 hours ago [-]
A lot of work has been done in Scala 3 to simplify everything.
And with the arrival of virtual threads in the JVM there are new concurrency libraries e.g. Ox [1] and Gears [2] which remove the need to use FP concepts like monads. Which have been the major source of much of the complexity.
For all its problems it is a seriously under-rated platform especially Scala.js which IMHO is far better and simpler than Typescript.
You're going to have that problem with any codebase written by people who don't particularly know the language. Typescript written by PHP programmers, Python written by Java programmers, you'll quickly get a huge impenetrable pile of crap.
You can optimize your codebase to be modified by an ever rotating group of people who don't fully understand it, or by a smaller group of people who do. Both are legitimate choices in specific contexts. But if you take a codebase written one way and try to maintain it the other way, your productivity will tank.
Sunscratch 11 hours ago [-]
Every <insert any language here> code base I have worked on, that wasnt written by small team of experts, turned into a huge pile of crap…
ldjkfkdsjnv 11 hours ago [-]
:-)
wtfparanoid 11 hours ago [-]
well aligned scala teams are a great thing, impenetrable code is not - maybe a poor choice of adjective?
henning 12 hours ago [-]
So behind the scenes, every one of those statements will make a whole new user object with a whole new address object so that it remains immutable? And whether that will actually have any real-world performance impact is I guess entirely situational. Still, what happens if you do that with a big object graph?
Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
Nullabillity 11 hours ago [-]
> Still, what happens if you do that with a big object graph?
The only thing that really matters here is how deep the graph is. Any unchanged object can just be reused as-is.
kelnos 12 hours ago [-]
This is in general how "mutations" are supposed to be done in a language like Scala (and is not unique to this library). Yes, Scala does have a set of mutable collections, but the immutable collections are heavily optimized to make creating a "new" collection with a mutation much cheaper than having to copy the entire collection.
Of course, copying a case class in order to change a field likely does require a full copy of the object, though since this is the JVM, things like strings can be shared between them.
Ultimately this pattern is... fine. Most uses don't end up caring about the extra overhead vs. that of direct mutation. I don't recall if the Scala compiler does this, but another optimization that can be used is to actually mutate an immutable object when the compiler knows the original copy isn't used anywhere else after the mutation.
> Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
That's one of the uses, but multiple ownership in general is another, without the presence of concurrency.
On top of that, there's the general belief (which I subscribe to) that mutation introduces higher cognitive load on someone understanding the code. Immutable data is much easier to reason about.
sriram_malhar 11 hours ago [-]
Yes, behind the scenes every one of those statements will make a shallow copy of the object. But it isn't just that object necessarily. For example, if you modify a tree node, then not only does that node needs cloning, its parent does too (since the modified parent needs to point to the new node), and so on until the root, which results in h = O(log(n)) new objects to create an entirely new tree. (h is the height of the tree).
What you get out if it is (a) safety, (b) understandability, which are wonderful properties to have as long as the end result is performing adequately. Implementing concurrent tree or graph traversals under conventional mutation is painful; the Java collection libraries simply throw a ConcurrentModificationException.
The equivalent code for readonly traversals of immutable data structures is simplicity itself. You also get versioning and undo's for free.
lmm 7 hours ago [-]
> So behind the scenes, every one of those statements will make a whole new user object with a whole new address object so that it remains immutable?
Not a "whole new" one since it will use shared references to the parts that didn't change (which is valid since they're immutable). And in principle the VM could even recognise that the new object is replacing the old one so it can be edited in place.
> Still, what happens if you do that with a big object graph?
I've literally never seen it cause a real-world performance problem, even if it theoretically could.
> Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
Partly that, but honestly mostly development sanity and maintainability. You can iterate a lot faster on immutable-first codebases, because it takes much less test coverage etc. to have the same level of confidence in your code.
valenterry 6 hours ago [-]
Your question is a bit like someone asking "so what does the garbage collector actually do? Does it X or Y? What impact does it have?"
And the answer is: no need to care about it. Unless you need to really optimize for high performance (not necessary in 99% of the cases, otherwise you'd use a different language from the beginning anyways).
> Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
One of the reasons is that you really can just completely stop thinking about it. Just like you can stop thinking of (de)allocations. Except for some edge-cases when performance matters a lot.
threeseed 11 hours ago [-]
> actually have any real-world performance impact
There are many techniques like this within Scala that would never be feasible if it wasn't for the fact that the JVM is ridiculously fast. You could write the worst code imaginable and in many cases would still have better performance than Python, Javascript etc.
Rendered at 08:54:53 GMT+0000 (UTC) with Wasmer Edge.
Conditional accessors have since become a popular language feature with e.g. the elvis operator ?. in C#. Optics make it possible to innovate on features like that in library code rather than as language features.
Something I've yet to see made into a language feature that is common in optics libraries are iterating accessors. E.g. to reset all counters we can, with optics, say something like (in Haskell syntax, since that is what I know)
and that sets the count to zero for all objects in the stats array. If not all stats objects have a count (some might be of a gauge type, for example) we can compose in a conditional accessor that resolves the count field only if it is not null: In the above statement, nothing is language syntax --it's all library functions and operators. But it still combines really well on the page. Once one knows optics, one rarely has to think very hard about how to do any get or update operation, even when the data types become complicated.You can think of this as the equivalent of a setter for immutable data structures.
This is an artificial example, because the cost of making a deep copy of this user structure is probably not that bad, and the boilerplate to make an efficient update is not all that bad, either. You would use an optics library when 1) you need efficient updates and 2) it's worth investing a little effort to hide the boilerplate.
Optics also let you concisely express access into a deeply nested structure, the getter paired with the setter. In my experience, updates are the motivation for setting up optics, and concise access is a nice thing you get as a bonus.
https://immerjs.github.io/immer/
https://en.wikipedia.org/wiki/Software_transactional_memory
The optical analogy comes from how these operations resemble zooming in on structures with a magnifying glass and the entire family of related transformations is called optics.
Asking as someone that doesn't use Scala at all, but has seen the hit-and-miss of some FP language LSPs.
https://gcanti.github.io/monocle-ts/
https://en.wikipedia.org/wiki/Optic_(disambiguation)
> In computer science, optics are a general class of bidirectional transformations
And with the arrival of virtual threads in the JVM there are new concurrency libraries e.g. Ox [1] and Gears [2] which remove the need to use FP concepts like monads. Which have been the major source of much of the complexity.
For all its problems it is a seriously under-rated platform especially Scala.js which IMHO is far better and simpler than Typescript.
[1] https://github.com/softwaremill/ox
[2] https://github.com/lampepfl/gears
You can optimize your codebase to be modified by an ever rotating group of people who don't fully understand it, or by a smaller group of people who do. Both are legitimate choices in specific contexts. But if you take a codebase written one way and try to maintain it the other way, your productivity will tank.
Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
The only thing that really matters here is how deep the graph is. Any unchanged object can just be reused as-is.
Of course, copying a case class in order to change a field likely does require a full copy of the object, though since this is the JVM, things like strings can be shared between them.
Ultimately this pattern is... fine. Most uses don't end up caring about the extra overhead vs. that of direct mutation. I don't recall if the Scala compiler does this, but another optimization that can be used is to actually mutate an immutable object when the compiler knows the original copy isn't used anywhere else after the mutation.
> Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
That's one of the uses, but multiple ownership in general is another, without the presence of concurrency.
On top of that, there's the general belief (which I subscribe to) that mutation introduces higher cognitive load on someone understanding the code. Immutable data is much easier to reason about.
What you get out if it is (a) safety, (b) understandability, which are wonderful properties to have as long as the end result is performing adequately. Implementing concurrent tree or graph traversals under conventional mutation is painful; the Java collection libraries simply throw a ConcurrentModificationException. The equivalent code for readonly traversals of immutable data structures is simplicity itself. You also get versioning and undo's for free.
Not a "whole new" one since it will use shared references to the parts that didn't change (which is valid since they're immutable). And in principle the VM could even recognise that the new object is replacing the old one so it can be edited in place.
> Still, what happens if you do that with a big object graph?
I've literally never seen it cause a real-world performance problem, even if it theoretically could.
> Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
Partly that, but honestly mostly development sanity and maintainability. You can iterate a lot faster on immutable-first codebases, because it takes much less test coverage etc. to have the same level of confidence in your code.
And the answer is: no need to care about it. Unless you need to really optimize for high performance (not necessary in 99% of the cases, otherwise you'd use a different language from the beginning anyways).
> Also, the original strong need for immutable data in the first place is safety under concurrency and parallelism?
One of the reasons is that you really can just completely stop thinking about it. Just like you can stop thinking of (de)allocations. Except for some edge-cases when performance matters a lot.
There are many techniques like this within Scala that would never be feasible if it wasn't for the fact that the JVM is ridiculously fast. You could write the worst code imaginable and in many cases would still have better performance than Python, Javascript etc.