3 ms·
This is immediately solved by Lenses _Object . key "them" . key "public_keys" . key "primary" . key "bundle" which you might complain saying that this is
by tel 13y ago
This is immediately solved by Lenses
_Object . key "them" . key "public_keys" . key "primary" . key "bundle"
which you might complain saying that this is yet another idiosyncratic method of traversing data types. In fact this whole thread is full of examples of other idiosyncratic methods of traversing (JSON only) data types.
Except lenses aren't. Lenses are highly principled and compose and combine in ridiculous ways while maintaining their exact behavior. Half the features proposed in this thread exist naturally and "emergently" by the nature of lenses. Finally, they follow mathematical laws describing how everything should work together all to the T.
At the highest level a Lens is a getter plus a setter bound together. You can extract either component to get or set a subpart of a value.
At the next level, you can take note that lenses "compose" (as a category) by letting you reach deeper and deeper into a structure. This is what I took advantage of with the JSON example above: composing them with `(.)`.
At the next level, lenses generalize naturally to "traversals" which target multiple subparts all at once and "folds" which build exotic "getters" over multiple targeted subparts.
At the next level, lenses "dualize" to prisms which deal with branching types. This is hard to explain if you haven't used a language with a true sum type (Scala, Haskell, ML, and lets not get into lazy/strict sums) but I used one above to traverse into the "Object" indicating failure if my assumption of the structure of the JSON blob were wrong.
At the next level you generalize these into isomorphisms which have nice mathematical properties determining when two types are identical by giving you invertible mappings between the two. This is like a lens which focuses on the entire type as its "subpart".
(http://hackage.haskell.org/package/lens http://hackage.haskell.org/package/lens)
---
At the end of the day there are even more steps in that hierarchy. It sounds ridiculously complex, and it is a little bit to learn. The advantage is, however, that the intuition of "focusing on a subpart" applies over any data type and with pretty near any weird combination of "lens-like" operators you can dream up.
---
Usually when someone first hears about lenses they say "Oh, getters and setters. I have those already, no big deal". That's really far from the case, however... Lenses end up being the XPath of everything.
- chriswarbo 13y agoI did think of lenses when I read this, but have yet to learn or use them so didn't want to give incorrect advice. Thanks for the overview :)
- nimish 13y agoYes this is the most practical use of lenses I've found. Entirely principled access to every flavor of object...multiple results even fall out naturally with traversals.
- jarrett 13y agoHaskell's Lens package (linked to above) is indeed nice. For a practical quickstart guide, I'de recommend the Github page over the Hackage page: https://github.com/ekmett/lens#lens-lenses-folds-and-traversals https://github.com/ekmett/lens#lens-lenses-folds-and-travers...
- tel 13y agoApologies for the self-plug, but I tried to write up a "friendly" intro to the Haskell lens library as well https://www.fpcomplete.com/school/to-infinity-and-beyond/pick-of-the-week/a-little-lens-starter-tutorial https://www.fpcomplete.com/school/to-infinity-and-beyond/pic...
- tstack 13y agoAny relation to lenses in http://augeas.net/ http://augeas.net/ ?
- tel 13y agoOn first glance it looks like they're drawing from the same source material even if they end up in different places.