1

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

However, there is no analog for takeTill. How can I implement this function takeTill1?

Note: This question intentionally shows no research effort as it was answered Q&A-Style. While this question is similar to this previous one the answer is not the same. I think the missing takeTill1 is a potential hurdle for beginners and this question therefore deserves a separate answer.

Community
  • 1
  • 1
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • 1
    To the downvoter: Please comment what you think is wrong with this question. Simply downvoting it, without explanation, will not make anything any better. I assume you're the person who serially downvotes my post. If you'd like something to be improved, I'd appreciate if you'd leave a comment instead of downvoting serially. – Uli Köhler Mar 27 '14 at 17:46

1 Answers1

2

You simply need to invert the predicate takeWhile1 takes (see this blogpost for a description on how to invert predicates if you are a beginner):

takeTill1 :: (Char -> Bool) -> Parser ByteString
takeTill1 p = takeWhile1 (not . p)

Also see this excellent answer providing a fast implementation for skipWhile1.

Community
  • 1
  • 1
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120