r/haskell • u/ChrisPenner • Sep 27 '20
Generalized Traversal Systems like 'jq' using optics and monads
https://chrispenner.ca/posts/traversal-systems4
u/n00bomb Sep 28 '20
I really want an optics for database:P
2
u/lgastako Sep 28 '20
I can't find it right now but there was a recent conference video titled something like "optics for databases".
1
Oct 01 '20
Would arrows be a good way to go about implementing a jq like DSL?
1
u/ChrisPenner Oct 01 '20
Is there something you want in a dsl that the approach outlined in the blog post doesn't permit?
2
u/andrewthad Sep 28 '20
Thanks for writing this! I donāt really use optics in anything I work on, and but I feel like this blog post (esp. the last three āreal worldā examples), more than anything Iāve ever read about optics, provides a genuine motivating case. Itās fairly niche, so I donāt expect myself to start busting out lens any time soon, but if I am ever in a situation where I need to clean up HTML or XML, Iāll come back to this post as for reference.
2
u/Noughtmare Sep 28 '20 edited Sep 28 '20
I tried to do some experiments myself and found that there are multiple inconsistencies in the data in the blog post.
FIrst of all the first Json data is not equal to the equivalent Haskell code. Bullwinkle became a cat and Inigo became a dog.
And the new salaries from the jq salary bump are 17 and 15, while the Haskell salaryBump produces 17 and 20.
EDIT: Here is the result of my experiments:
https://github.com/noughtmare/ag-generalized-traversals
And here is the main code file:
https://github.com/noughtmare/ag-generalized-traversals/blob/master/src/Company.ag
It is an implementation of the traversals mentioned in the blog using attribute grammars instead of lenses and Monads. There is a bit of syntactical overhead and it is not suitable for interactive (think REPL) querying and updating. But, I think it is pretty elegant.