codewars / runner

Issue tracker for Code Runner
33 stars 8 forks source link

Add elm/parser for elm #309

Open samy-jaziri opened 3 months ago

samy-jaziri commented 3 months ago

This is a useful elm package for parsing strings, replacing in an original way regexp manipulations. This is a standard elm package, depending only on elm/core.
Practicing elm/parser for every kata which are parsing related, is the best way to get familiar to this core package of the elm language.

For example, in the Hidden "Cubic" numbers kata, char by char manipulation, or regexp manipulation, to parse 3-digit numbers verifying isDigit predicate could be done by a recursive Parser implemented by :

numbers : Parser.Parser ( List String )
numbers = 
  Parser.oneOf 
  [ Parser.succeed (\i l -> if isCubic i then i::l else l )
      |= Parser.getChompedString 
        <| ( Parser.succeed () 
              |. Parser.chompIf Char.isDigit 
              |. Parser.chompIf Char.isDigit 
              |. Parser.chompIf Char.isDigit 
            ) 
      |= Parser.lazy ( \_ -> numbers)
  , Parser.succeed [] 
      |. Parser.end
  , Parser.succeed identity
      |. Parser.chompWhile (Char.isDigit >> not)
      |= Parser.lazy ( \_ -> numbers)
  ]

:+1: reaction might help to get this request prioritized.