Parsing Example in Elm

Anyone willing to give some feedback on my simple parsing kata?

Perhaps make it stricter? Using an ADT is an obvious one…

There’s non-parsing code which can obviously be improved, but I am into parsers right now…

You can use symbol or keyword (depending on what kind of error message you want on failure) to parse a string at once instead of parsing it character by character. Here’s how I would rewrite the parser while keeping the rest of the code unchanged:

parse =
    "IV IX XL XC CD CM I V X L C D M"
        |> String.split " "
        |> List.map (\string -> succeed (always string) |= symbol string)
        |> Parser.oneOf
        |> repeat oneOrMore

Oh, that’s nice - using symbol & keyword with succeed like that.

What I wanted to do was also return ADTs, rather than strings, just helps type check everything…

Here’s how you can return ADTs:

parse =
    [ IV, IX, XL, XC, CD, CM, I, V, X, L, C, D, M ]
        |> List.map (\rn -> succeed (always rn) |= symbol (toString rn))
        |> Parser.oneOf
        |> repeat oneOrMore

You just need to remove the quotes around the strings in the sum function now and it should work!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.