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.
I decided to write it myself for fun. I decided that "From Scratch" means:
No parser libraries (parsec/happy/etc)
No using read from Prelude
No hacky meta-parsing
Here is what I came up with (using my favourite parsing method: parser combinators):
import Control.Monad ((>=>), replicateM)
import Control.Applicative (Alternative (..), asum, optional)
import Data.Maybe (fromMaybe)
import Data.Functor (($>))
import Data.List (singleton)
import Data.Map (Map, fromList)
import Data.Bifunctor (first, second)
import Data.Char (toLower, chr)
newtype Parser i o = Parser { parse :: i -> Maybe (i, o) } deriving (Functor)
instance Applicative (Parser i) where
pure a = Parser $ \i -> Just (i, a)
a <*> b = Parser $ parse a >=> \(i, f) -> second f <$> parse b i
instance Alternative (Parser i) where
empty = Parser $ const Nothing
a <|> b = Parser $ \i -> parse a i <|> parse b i
instance Monad (Parser i) where
a >>= f = Parser $ parse a >=> \(i, b) -> parse (f b) i
instance Semigroup o => Semigroup (Parser i o) where
a <> b = (<>) <$> a <*> b
instance Monoid o => Monoid (Parser i o) where
mempty = pure mempty
type SParser = Parser String
charIf :: (a -> Bool) -> Parser [a] a
charIf cond = Parser $ \i -> case i of
(x:xs) | cond x -> Just (xs, x)
_ -> Nothing
char :: Eq a => a -> Parser [a] a
char c = charIf (== c)
one :: Parser i a -> Parser i [a]
one = fmap singleton
str :: Eq a => [a] -> Parser [a] [a]
str = mapM char
sepBy :: Parser i a -> Parser i b -> Parser i [a]
sepBy a b = (one a <> many (b *> a)) <|> mempty
data Decimal = Decimal { mantissa :: Integer, exponent :: Int } deriving Show
data JSON = Object (Map String JSON) | Array [JSON] | Bool Bool | Number Decimal | String String | Null deriving Show
whitespace :: SParser String
whitespace = many $ asum $ map char [' ', '\t', '\r', '\n']
digit :: Int -> SParser Int
digit base = asum $ take base [asum [char c, char (toLower c)] $> n | (c, n) <- zip (['0'..'9'] <> ['A'..'Z']) [0..]]
collectDigits :: Int -> [Int] -> Integer
collectDigits base = foldl (\acc x -> acc * fromIntegral base + fromIntegral x) 0
unsignedInteger :: SParser Integer
unsignedInteger = collectDigits 10 <$> some (digit 10)
integer :: SParser Integer
integer = asum [char '-' $> (-1), char '+' $> 1, str "" $> 1] >>= \sign -> (sign *) <$> unsignedInteger
-- This is the ceil of the log10 and also very inefficient
log10 :: Integer -> Int
log10 n
| n < 1 = 0
| otherwise = 1 + log10 (n `div` 10)
jsonNumber :: SParser Decimal
jsonNumber = do
whole <- integer
fraction <- fromMaybe 0 <$> optional (str "." *> unsignedInteger)
e <- fromIntegral . fromMaybe 0 <$> optional ((str "E" <|> str "e") *> integer)
pure $ Decimal (whole * 10^log10 fraction + signum whole * fraction) (e - log10 fraction)
escapeChar :: SParser Char
escapeChar = char '\\'
*> asum [
str "'" $> '\'',
str "\"" $> '"',
str "\\" $> '\\',
str "n" $> '\n',
str "r" $> '\r',
str "t" $> '\t',
str "b" $> '\b',
str "f" $> '\f',
str "u" *> (chr . fromIntegral . collectDigits 16 <$> replicateM 4 (digit 16))
]
jsonString :: SParser String
jsonString =
char '"'
*> many (asum [charIf (\c -> c /= '"' && c /= '\\'), escapeChar])
<* char '"'
jsonObjectPair :: SParser (String, JSON)
jsonObjectPair = (,) <$> (whitespace *> jsonString <* whitespace <* char ':') <*> json
json :: SParser JSON
json =
whitespace *>
asum [
Object <$> fromList <$> (char '{' *> jsonObjectPair `sepBy` char ',' <* char '}'),
Array <$> (char '[' *> json `sepBy` char ',' <* char ']'),
Bool <$> asum [str "true" $> True, str "false" $> False],
Number <$> jsonNumber,
String <$> jsonString,
Null <$ str "null"
]
<* whitespace
main :: IO ()
main = interact $ show . parse json
This parses numbers as my own weird Decimal type, in order to preserve all information (converting to Double is lossy). I didn't bother implementing any methods on the Decimal, 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.
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.
when you burst into the wedding promising doom and death, we didn't kick you out because we didn't believe you, we kicked you out because we already knew. i mean, i'm involved let's be real.
You just need to find a girl that also likes Tsoding! Then, you can ask her "Hey, do you have plans for Christmas? I'd love it if we could do AoC (Advent of Code) in a language we both hate!"
Don't worry. I mean nothing but humour by it. I myself am married to a physicist who takes absolutely no interest whatsoever in programming, but can talk happily for ages about something weird they found.
There are those who transition, so a significant chunk of that male programmer population is "male" as in quotation marks, only that some transition earlier than others. Does not guarantee that you can get the transgender autistic puppygirl (or other variations) of your dreams, since many of them are lesbians.
But also feel free to look outside your field for a partner. It's okay to date an artist as a programmer.
It's odd in the Australian public service, with COBOL programmers. They've been in the job long enough that they started when the public service was the only employer who would employ women as programmers. I'm on the systems analyst side of the fence, the programmers I have worked with include a bit more than 60% women
I think all the programmers I know are married or gay or not interested. I think the gay ones are mostly married too.
Eh, it's just different. Other languages are hard in other ways. Haskell's at least have very good reason behind them.
I write Haskell professionally and and am teaching to people without any experience, and it's really no different than anything else. Though I will say that my experience is that university professors are often pretty clueless about the language and don't teach it well.
I think it's the paradigm change. Most people including myself learnt some kind of procedural language in school, shifting towards functional thinking is just very difficult. But of course that's a skill a computer scientist must have and one of the reasons I didn't graduate.
91 Comments
balsoft@lemmy.ml · 102 pts · 286d
You gotta admit though, Haskell is crazy good for parsing and marshaling data
marcos@lemmy.world · 43 pts · 286d
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.
balsoft@lemmy.ml · 22 pts · 286d
You could probably write a very basic parser combinator library, enough to parse JSON, in 100 lines of Haskell
someacnt@sh.itjust.works · 11 pts · 286d
Judging by the Parser newtype, he did.
balsoft@lemmy.ml · 18 pts · 285d
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.
jerkface@lemmy.ca · 7 pts · 285d
So, ARE you bringing a girl?
balsoft@lemmy.ml · 16 pts · 285d
I'm not coming to my parents for this new year's because I might get arrested and/or sent to die in a war. But once Putin dies, yes, I am
jerkface@lemmy.ca · 6 pts · 285d
So that's two things to look forward to!
cassandrafatigue@lemmy.dbzer0.com · 3 pts · 284d
Didn't know where you were talking about til you said Putin.
join@lemmy.ml · 10 pts · 286d
With recursive list comprehensions you can cram quite some complexity into one line of code.
expr@programming.dev · 4 pts · 285d
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.
davidagain@lemmy.world · 3 pts · 285d
Haskell is succinct.
ultimate_worrier@lemmy.dbzer0.com · 2 pts · 209d
hakunawazo@lemmy.world · 1 pts · 285d
jenesaisquoi@feddit.org · 1 pts · 285d
serde has entered the chat
fuck_u_spez_in_particular@lemmy.world · 7 pts · 285d
From Scratch (as much as I like Rust, it's very likely more verbose from scratch). Haskell is perfect for these kinds of things.
jenesaisquoi@feddit.org · 5 pts · 285d
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!
balsoft@lemmy.ml · 2 pts · 285d
Personally I'm more partial to nom. Serde is quite verbose and complex for a parser.
magic_smoke@lemmy.blahaj.zone · 84 pts · 286d
Jokes on her, I've transitioned since last Christmas.
Fisherswamp@programming.dev · 82 pts · 286d
You can still bring a girl though
magic_smoke@lemmy.blahaj.zone · 2 pts · 281d
Mission failed successfully: I'm bringing my enby instead and you can't stop me.
lord_ryvan@ttrpg.network · 1 pts · 276d
If your mum is not OK with this, can I be your adoptive parent?
chellomere@lemmy.world · 62 pts · 286d
I am the girl! Hmm, but maybe I'll bring another one too? 🤔
bhamlin@lemmy.world · 14 pts · 286d
The more the merrier!
HeyThisIsntTheYMCA@lemmy.world · 4 pts · 285d
it's yuletide! everyone (except that person. they know what they did) is welcome and celebrated!
cassandrafatigue@lemmy.dbzer0.com · 2 pts · 284d
Not sorry
HeyThisIsntTheYMCA@lemmy.world · 2 pts · 284d
when you burst into the wedding promising doom and death, we didn't kick you out because we didn't believe you, we kicked you out because we already knew. i mean, i'm involved let's be real.
CanadaPlus@lemmy.sdf.org · 80 pts · 286d
Who needs a girl when you have monads to keep you warm?
boonhet@sopuli.xyz · 20 pts · 286d
Or become a girl with gonads
tiramichu@sh.itjust.works · 63 pts · 285d
No mom, I'm gonna BE a girl for Christmas. puts on programming socks
Quibblekrust@thelemmy.club · 5 pts · 284d
That's 100% how I read it at first.
yetAnotherUser@lemmy.ca · 38 pts · 286d
You just need to find a girl that also likes Tsoding! Then, you can ask her "Hey, do you have plans for Christmas? I'd love it if we could do AoC (Advent of Code) in a language we both hate!"
Gumbyyy@lemmy.world · 10 pts · 286d
Well shit, I've never seen AoC before - I'm not usually very interested in programming just for fun, but I might give that a try!
Deathray5@lemmynsfw.com · 2 pts · 285d
I only was shown it a few weeks ago so seeing it here was a bit of a shock
Cevilia@lemmy.blahaj.zone · 37 pts · 285d
ILikeBoobies@lemmy.ca · 14 pts · 285d
Wouldn’t it hit the same as it would a straight male?
buddascrayon@lemmy.world · 19 pts · 285d
POV: Not all moms are accepting of their daughters being into girls.
lemmydividebyzero@reddthat.com · 36 pts · 286d
There are far more male programmers... As a programmer, be gay or stay alone... Choose!
captainlezbian@lemmy.world · 31 pts · 286d
Oh that explains why my wife is gay
mathemachristian@lemmy.blahaj.zone · 13 pts · 285d
If she was around the same cs students as me then yeah
davidagain@lemmy.world · 3 pts · 285d
Ouch!
Agent641@lemmy.world · 8 pts · 285d
She sleeps with men, that's pretty gay
captainlezbian@lemmy.world · 31 pts · 285d
There are a lot of things she does but that aint one of them
MrScottyTay@sh.itjust.works · 8 pts · 285d
Think you forgot to check their username before commenting that haha
cassandrafatigue@lemmy.dbzer0.com · 1 pts · 284d
Thats a hell of a way to find out.
cassandrafatigue@lemmy.dbzer0.com · 1 pts · 284d
And clearly it worked!
undefined@lemmy.hogru.ch · 25 pts · 285d
Can programmers only be with other programmers or am I missing something?
davidagain@lemmy.world · 23 pts · 285d
"JSON parser 100% from scratch in Haskell in 110 lines" doesn't get you horny? I guess some people are just wired differently.
undefined@lemmy.hogru.ch · 4 pts · 285d
I’m a programmer myself but my wife isn’t a programmer, that was my motivation for questioning.
davidagain@lemmy.world · 5 pts · 285d
Don't worry. I mean nothing but humour by it. I myself am married to a physicist who takes absolutely no interest whatsoever in programming, but can talk happily for ages about something weird they found.
cassandrafatigue@lemmy.dbzer0.com · 4 pts · 284d
Oh, is she, like, an EE or something? Maybe a web designer?
lemmydividebyzero@reddthat.com · 11 pts · 285d
But you kind of have to leave the house for that... I mean... We talk about programmers....
/s
fibojoly@sh.itjust.works · 2 pts · 284d
Well yeah, obv. But not enough girls in computer science, so like the fishes, some of them magically turn into girls after a while.
ZILtoid1991@lemmy.world · 18 pts · 286d
There are those who transition, so a significant chunk of that male programmer population is "male" as in quotation marks, only that some transition earlier than others. Does not guarantee that you can get the transgender autistic puppygirl (or other variations) of your dreams, since many of them are lesbians.
But also feel free to look outside your field for a partner. It's okay to date an artist as a programmer.
rucksack@feddit.org · 8 pts · 285d
I think programmer should be seen as a gender itself.
I'm currently transitioning myself, already have a homeserver and a Linux PC, can't wait to be a real programmer.
lessthanluigi@lemmy.sdf.org · 9 pts · 285d
I detransitioned from being a programmer and all I have is depression since, maybe I should retransission into being a programmer
shoki@lemmy.world · 7 pts · 285d
and gender confirmation would not be getting called sir/ma'am at the starbucks but people asking you for IT help?
cassandrafatigue@lemmy.dbzer0.com · 3 pts · 284d
Just slurs, shouted angrily and incoherently whike they blame you for all the shit that was designed (by someone else) to not work.
andioop@programming.dev · 3 pts · 283d
Feels weird reading this as the only single woman programmer in my friend group who likes men
daniskarma@lemmy.dbzer0.com · 10 pts · 286d
It's not gay if I'm wearing programming socks.
psud@aussie.zone · 4 pts · 284d
It's odd in the Australian public service, with COBOL programmers. They've been in the job long enough that they started when the public service was the only employer who would employ women as programmers. I'm on the systems analyst side of the fence, the programmers I have worked with include a bit more than 60% women
I think all the programmers I know are married or gay or not interested. I think the gay ones are mostly married too.
RedSnt@feddit.dk · 27 pts · 286d
Prisoner of war?
gigastasio@sh.itjust.works · 11 pts · 286d
massive_bereavement@fedia.io · 4 pts · 286d
Pupil of Women
whyNotSquirrel@sh.itjust.works · 8 pts · 286d
not sure if it's sarcastic: point of view
AugustWest@lemmy.world · 12 pts · 286d
Title is edited
RedSnt@feddit.dk · 6 pts · 286d
I did consider taking a screenshot before the edit from POW to POV, but eh.
whyNotSquirrel@sh.itjust.works · 3 pts · 285d
ohhhh, I'm stupid... because it made me think that POV had two meanings possible.... like... Prisoner Of Var.....
knexcar@lemmy.world · 1 pts · 285d
Prisoner of Vore? Seems kinda hot
jerkface@lemmy.ca · 24 pts · 285d
I don't think "programmer" fully captures the reality of being an emacs-based programmer.
MonkderVierte@lemmy.zip · 23 pts · 286d
Add those few lines, will ya?
ZILtoid1991@lemmy.world · 10 pts · 286d
But that would break the 111 line rule.
MonkderVierte@lemmy.zip · 4 pts · 286d
There's a rule?
Not anymore.
smiletolerantly@awful.systems · 17 pts · 286d
Prisoner Of War:
ZILtoid1991@lemmy.world · 7 pts · 286d
There was no ESL moment in Ba Sing Se!
TheLeadenSea@sh.itjust.works · 3 pts · 286d
Eastern Sign Language?
ZILtoid1991@lemmy.world · 3 pts · 286d
English Second Language
FiskFisk33@startrek.website · 17 pts · 286d
I wouldn't trust a guy letting their battery go that low either
umbraroze@slrpnk.net · 13 pts · 285d
I'm a girl. I'm not interested in Haskell, that's too frigging endofunctiorific. Erlang! That's what all the cool guys are doing.
ICastFist@programming.dev · 4 pts · 284d
What about going an extra step into Elixir?
gigachad@piefed.social · 11 pts · 286d
A JSON parser in Haskell, what a day to have eyes
expr@programming.dev · 1 pts · 285d
?
Haskell's incredibly good for writing parsers.
gigachad@piefed.social · 1 pts · 285d
But oh boy is it difficult. We started with Haskell in the first semester CS and it was a pain. Kudos to anyone seriously developing in Haskell.
expr@programming.dev · 3 pts · 285d
Eh, it's just different. Other languages are hard in other ways. Haskell's at least have very good reason behind them.
I write Haskell professionally and and am teaching to people without any experience, and it's really no different than anything else. Though I will say that my experience is that university professors are often pretty clueless about the language and don't teach it well.
gigachad@piefed.social · 3 pts · 285d
I think it's the paradigm change. Most people including myself learnt some kind of procedural language in school, shifting towards functional thinking is just very difficult. But of course that's a skill a computer scientist must have and one of the reasons I didn't graduate.
Kolanaki@pawb.social · 10 pts · 285d
He won't be done debugging her by then. She'll be ready for beta testing next year.
bestelbus22@lemmy.world · 9 pts · 285d
Hello everyone, and welcome to yet another recreational programming session with who?
edinbruh@feddit.it · 5 pts · 286d
If you are writing a parser in haskell just use Happy and get it over with
davidagain@lemmy.world · 1 pts · 285d
It's a parser for json. Import Aeson and derive one for your custom type in a single line!
mudkip@lemdro.id · 4 pts · 285d
Well, JSON is an easy format to parse. The spec can fit onto one page.
biggeoff@sh.itjust.works · 4 pts · 284d
Based Tsoding
goatinspace@feddit.org · 4 pts · 286d
AI girlfriend
psud@aussie.zone · 1 pts · 284d
If it scratches an itch. Mother probably wants grandchildren though
luciferofastora@feddit.org · 3 pts · 281d
Why is nobody commenting on the phone battery?