purescript-contrib/purescript-string-parsers

count, skip and many1Till combinators

Closed this issue · 2 comments

I found I needed these for my MIDI parser:

-- | Parse `n` occurences of `p`. -
count :: forall a. Int -> Parser a -> Parser (List a)
count n p =
  let
    accumulate x acc =
      if x <= 0 then
        pure (reverse acc)
      else do
        res <- p
        accumulate (x - 1) (res : acc)
  in
    accumulate n Nil

-- | Apply a parser and skip its result.
skip :: forall a. Parser a -> Parser Unit
skip p = unit <$ p

-- | Parse several phrases until the specified terminator matches, requiring at least one match.
many1Till :: forall a end. Parser a -> Parser end -> Parser (List a)
many1Till p end = do
  x <- p
  xs <- manyTill p end
  pure (x:xs)

Any chance of accepting another PR ?

paf31 commented

count is just replicateA and skip is void. many1Till sounds good though.

Ah - I didn't realise that - still a good deal to learn. I'll raise a PR for many1Till.