Skip to content

Instantly share code, notes, and snippets.

@aycanirican
Created November 8, 2010 10:05
Show Gist options
  • Select an option

  • Save aycanirican/667540 to your computer and use it in GitHub Desktop.

Select an option

Save aycanirican/667540 to your computer and use it in GitHub Desktop.
attoparsec additions
-- | Match a parser at least @N@ times.
manyN :: Int -> Parser a -> Parser [a]
manyN n p
| n <= 0 = return []
| otherwise = (++) <$> count n p <*> many p
{-# INLINE manyN #-}
-- | Match a parser at least @N@ times, but no more than @M@ times.
manyNtoM :: Int -> Int -> Parser a -> Parser [a]
manyNtoM n m p
| n < 0 = return []
| n > m = return []
| n == m = count n p
| n == 0 = do foldr (<|>) (return []) (map (\x -> try $ count x p) (Prelude.reverse [1..m]))
| otherwise = (++) <$> count n p <*> manyNtoM 0 (m - n) p
{-# INLINE manyNtoM #-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment