3 ms·
> What's really neat about lenses to me (more of a practitioner than a theoretician) is that while the level 0 explanation of them is "they're like getters and
by avgcorrection 4y ago
> What's really neat about lenses to me (more of a practitioner than a theoretician) is that while the level 0 explanation of them is "they're like getters and setters in your OOP language" they extend the concept much further.
This doesn’t tell me anything since getters/setters are almost useless in my experience.
- fho 4y agoRight ... And lenses are not. Imho a better comparison would be to (safe) pointers that can point to multiple, conditional positions in a data structure. Example: in a game you want to update the health points of all enemies that are affected by an area of effect attack. With lenses you would compose a lens from multiple simpler lenses: ```enemiesAtDistanceToPoint pos radius = enemies . filtered (distanceTo pos . (< radius))``` and then you could modify those enemies: ```enemiesAtDistanceToPoint (10,20) 5 . health -= 5``` The benefit here is that you abstract the work of extracting the enemies and can reuse it elsewhere. Also note the lack of any loop to affect multiple entities.
- c-cube 4y agoWould multiple-cursors editing be an apt comparison? :-)
- fho 4y agoSomewhat, but even my pointer analogy just scratches the surface. Filtering/selecting is big, but another interesting thing is that there are lenses (which I think are called prisms?) that only provide a view of something. Eg you have a text document in Unicode, select all words marching a certain pattern, and then look as if they were encoded in ASCII. (No idea why this would useful :-) ).
- hither_shores 4y agoA "lens" that only provides a view of something is just a plain function. Prisms are first class constructors: a `Prism a b` can build a `b` from an `a`, or attempt to match on a `b` and `Maybe` get an `a` that could construct it.
- fho 4y agoHmm yes ... maybe the word "view" was not chosen well. It's more of a "there and back again" thing.
- garethrowlands 4y agoThe OP means accessing instance variables really. That is, the `.x` in `this.x` or `that.x`. They're not making a distinction between manually created getters and setters and what the compiler generates.
- avgcorrection 4y agoHaskell has the same means of accessing records as Java has of accessing its fields. Or no?
- kqr 4y agoSure, but compared to lenses it's like writing in gotos instead of function calls.