I know it's a bit late in the peace for AoC, but if anyone still needs an Int parser or wants to give it a go... just thought I would share.
digit : Parser (List U8) Nat
digit =
input <- buildPrimitiveParser
when input is
['0',..] -> Ok { val: 0, input: List.dropFirst input }
['1',..] -> Ok { val: 1, input: List.dropFirst input }
['2',..] -> Ok { val: 2, input: List.dropFirst input }
['3',..] -> Ok { val: 3, input: List.dropFirst input }
['4',..] -> Ok { val: 4, input: List.dropFirst input }
['5',..] -> Ok { val: 5, input: List.dropFirst input }
['6',..] -> Ok { val: 6, input: List.dropFirst input }
['7',..] -> Ok { val: 7, input: List.dropFirst input }
['8',..] -> Ok { val: 8, input: List.dropFirst input }
['9',..] -> Ok { val: 9, input: List.dropFirst input }
_ -> Err (ParsingFailure "not a digit")
int : Parser (List U8) I64
int =
const (\maybeDash -> \digits ->
sign = when maybeDash is
Ok _ -> -1
Err _ -> 1
List.walk digits 0 (\sum, d -> sum * 10 + d)
|> Num.toI64
|> Num.mul sign
)
|> keep (maybe (codeunit '-'))
|> keep (oneOrMore digit)
if you want to, I think you can write that pattern match as
when input is
[ c, .. ] if c >= '0' && c <= '9' -> Ok { val: c - '0', input: List.dropFirst input }
_ -> ...
Slight modification Ok { val: Num.toNat (c - '0') ...
works like a charm.
Last updated: Jul 06 2025 at 12:14 UTC