3 ms·
Lens is difficult to get your head around. It is simultaneously a rather simple idea ("bundle getters and setters together... ") and a ridiculously sophisticate
by tel 12y ago
Lens is difficult to get your head around. It is simultaneously a rather simple idea ("bundle getters and setters together... ") and a ridiculously sophisticated and generalized implementation ("... in every conceivable lawful way such that they all work together as they ought to!").
You probably do want lenses. Learning to use them is its own "mind blowing" experience similar to learning functional programming itself. Better, they integrate smoothly with Haskell so once you've learned them the cost of using them is quite small.
Unfortunately, learning lenses from `lens` itself is quite hard. There is some amount of training materials (shameless plug here)
https://www.fpcomplete.com/user/tel/a-little-lens-starter-tutorial
https://www.fpcomplete.com/user/tel/lens-aeson-traversals-prisms
but the library itself is typically targeting a proficient user far better than a new one. I'd recommend trying to implement your own lenses as a learning task. If you don't bother generalizing it as far as Ed has then the core concept is very simple. I wrote up a CodeWars task for this using the Van Laarhoven style used in `lens`, but there are even simpler forms as well.
http://www.codewars.com/kata/54258ffb430ca2e4b5000239
- rnhmjoj 12y agoThank you.
- Tyr42 12y agoThere's also simpler packages, right? Targeted at having less power, but being more understandable as a result. Was lens-family one of the simpler ones? https://hackage.haskell.org/package/lens-family
- tel 12y agoThere are a lot of lens packages. I'd roughly split them into the "Van Laarhoven style" and the "Abstract style". In the Abstract Style there is `data-lens` and `fclabels`. These use some kind of packaged form of (getter,setter) and require a Category instance to compose using (.). They are all mutually incompatible with one another. In the Van Laarhoven Style are `lens`, `lens-family`, and `lens-family-core` of which the last is the simplest. These are all mutually compatible, and compose automatically using regular function composition and the Prelude version of (.), but they're somewhat hairy to see at first since they must expose their underlying type (for subtyping purposes) and that one is pretty unexpectedly weird. I recommend using Van Laarhoven style lenses. If you really want an abstract style lens, fclabels is the best I've been told.