Implementing skipWhile1 in attoparsec

血红的双手。 提交于 2019-12-08 02:47:25

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/22613161/implementing-skipwhile1-in-attoparsec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!