News for hackers. Stuff that matters.
Re:Parser Combinators in Haskell by cosmic_quanta on Wednesday December 22, 2021 @09:29AM (#29649771)
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-svg

I haven't needed to do the same with SQL but I imagine that this already exists as well

Re:Parser Combinators in Haskell by jy14898 on Wednesday December 22, 2021 @09:50AM (#29650053)
Along the same line, there's also invertible syntax descriptions https://gist.github.com/tfausak/a8d7f135bf76e64ea6f35d3be692... , which allow one definition to create both a parser and pretty-printer
Re:Parser Combinators in Haskell by jayshua on Wednesday December 22, 2021 @10:20AM (#29650435)
I 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/

Re:Parser Combinators in Haskell by marginalia_nu on Wednesday December 22, 2021 @11:07AM (#29651149)
I'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.

Re:Parser Combinators in Haskell by CornCobs on Wednesday December 22, 2021 @10:26AM (#29650527)
What 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

Re:Parser Combinators in Haskell by jaspervdj on Wednesday December 22, 2021 @10:47AM (#29650826)
The 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.

Re:Parser Combinators in Haskell by louthy on Wednesday December 22, 2021 @10:40AM (#29650723)
You can pass contextual state/environment in the monad for trivial data binding