3 ms·
I love parser combinators. It's a much more natural 'format' than parsing with regular expressions which I have to do at $DAY_JOB. One aspect that I see discus
by cosmic_quanta 5y ago
I love parser combinators. It's a much more natural 'format' than parsing with regular expressions which I have to do at $DAY_JOB.
One aspect that I see discussed less often is the idea of 'builder combinators', which are kind of the inverse of parser combinators. There are multiple libraries in Haskell which make it very easy to write HTML/SVG/... this way:
https://hackage.haskell.org/package/blaze-html https://hackage.haskell.org/package/blaze-html
https://hackage.haskell.org/package/blaze-svg https://hackage.haskell.org/package/blaze-svg
I haven't needed to do the same with SQL but I imagine that this already exists as well
- jy14898 5y agoAlong the same line, there's also invertible syntax descriptions https://gist.github.com/tfausak/a8d7f135bf76e64ea6f35d3be692cbeb https://gist.github.com/tfausak/a8d7f135bf76e64ea6f35d3be692... , which allow one definition to create both a parser and pretty-printer
- jayshua 5y agoI like using Elm's JSON library as an example because it's very small. Since there are only a handful of functions you can understand the entire thing pretty easily. Plus, encoding JSON is a great jump-off into the decoding half of the library which is basically a simple parser combinator system. https://package.elm-lang.org/packages/elm/json/latest/ https://package.elm-lang.org/packages/elm/json/latest/
- CornCobs 5y agoWhat are the benefits of these monadic builder interfaces instead of simply pure functions? One can imagine a simple Html type with html tag functions e.g. `div :: [Attributes] -> [Html] -> Html` Instead of using do notation to build up multiple children one would simply supply a list of children. Added benefit of using simpler map/filter/filterMap/mapMaybe vs their monadic counterparts
- louthy 5y agoYou can pass contextual state/environment in the monad for trivial data binding
- jaspervdj 5y agoThe builder interface has important performance benefits over working with lists: similar to e.g. concatenating many small Strings vs using StringBuilder in Java. The builder interface indeed does not need to be monadic, you can simply append things using <> and get the same benefits. The monadic interface is purely a syntactic enhancement on top of that, to reduce the number of operators you need.
- marginalia_nu 5y agoI'm normally a recursive descent-man, but I implemented parsec-style parsing in Java once, just because I was curious to see what the world looked like when it was on fire. I don't have the code as this was just me wasting company time during a hackathon they insisted we all attended. I did get it working, despite the type system I was working in. But yeah. Mistakes were made.