parser-combinators icon indicating copy to clipboard operation
parser-combinators copied to clipboard

[proposal] sepByN

Open epoberezkin opened this issue 5 years ago • 3 comments

Thanks for the library!

I was looking for something like sepBy but requiring an exact number of separated elements.

If it's not here yet, maybe it could be added:

sepByN :: Alternative m => m a -> m sep -> Int -> m [a]
sepByN x sep n = (:) <$> x <*> count (n-1) (sep *> x)

It can be used in this "semantic" way:

n & x `sepByN` sep

epoberezkin avatar Jun 07 '20 21:06 epoberezkin

I'm just wondering how common the idiom is. There is a balance between providing all possible combinators (we can sure define many) and keeping the library reasonably slim.

mrkkrp avatar Jun 08 '20 09:06 mrkkrp

I understand. Probably best to keep the issue open to see if there is any interest.

epoberezkin avatar Jun 08 '20 09:06 epoberezkin

I just wrote this exact function with the exact same name, and came here to propose adding it. Lo and behold, it was one of the 3 open feature requests 😄

sepByN :: Int -> Parser a -> Parser sep -> Parser [a]
sepByN n p sep = p <:> replicateM (n-1) (sep *> p)
  where (<:>) = liftA2 (:)

Use case was parsing 5*5 bingo boards for advent of code 2021 like this

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9

sullyj3 avatar Oct 08 '22 08:10 sullyj3