839
POV: You're a programmer
(lemmy.world)
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
You gotta admit though, Haskell is crazy good for parsing and marshaling data
Yes. I'm divided into "hum... 100 lines is larger than I expected" and "what did he mean 'from scratch'? did he write the parser combinators? if so, 100 lines is crazy small!"
But I'm settling in believing 80 of those lines are verbose type declarations.
You could probably write a very basic parser combinator library, enough to parse JSON, in 100 lines of Haskell
Judging by the Parser newtype, he did.
I decided to write it myself for fun. I decided that "From Scratch" means:
readfrom PreludeHere is what I came up with (using my favourite parsing method: parser combinators):
This parses numbers as my own weird
Decimaltype, in order to preserve all information (converting toDoubleis lossy). I didn't bother implementing any methods on theDecimal, because there are other libraries that do that and we're just writing a parser.It's also slow as hell but hey, that's naive implementations for you!
It ended up being 113 lines. I think I could reduce it a bit more if I was willing to sacrifice readability and/or just inline things instead of implementing stdlib typeclasses.
With recursive list comprehensions you can cram quite some complexity into one line of code.
Just looking at the image, yeah he's a little parser combinator library entirely from scratch.
Not sure what you mean by verbose type declarations. It looks to be 2 type declarations in a few lines of code (a newtype for the parser and a sum type to represent the different types of JSON values). It's really not much at all.
Haskell is succinct.
serde has entered the chat
Personally I'm more partial to nom. Serde is quite verbose and complex for a parser.
From Scratch (as much as I like Rust, it's very likely more verbose from scratch). Haskell is perfect for these kinds of things.
I will concede that implementing the first version in Haskell would be better.
Mostly so that we can then fulfil the meme of reimplementing it in Rust!