Implementing skipWhile1 in attoparsec

2019-05-29 02:14发布

Attoparsec provides the function takeWhile1 that consumes at least one character.

However, there is no analog for skipWhile. How can I implement this function skipWhile1?

Note: This question intentionally shows no research effort as it was answered Q&A-Style.

2条回答
Emotional °昔
2楼-- · 2019-05-29 02:51

Another possible implementation:

import Control.Applicative

skipWhile1 p = skip p *> skipWhile p

This might actually be faster than @Uli's answer because takeWhile builds a result string, whereas skipWhile doesn't. Laziness might make them equivalent (ie. maybe takeWhile doesn't actually build the string if you don't use it); I can't test at the moment to verify this.

查看更多
虎瘦雄心在
3楼-- · 2019-05-29 02:51

You can use Control.Monad.void together with takeWhile1 to simply ignore the result:

import Data.Attoparsec.Char8
import Control.Monad (void)

skipWhile1 :: (Char -> Bool) -> Parser ()
skipWhile1 = void . takeWhile1
查看更多
登录 后发表回答