parser icon indicating copy to clipboard operation
parser copied to clipboard

keepOne : (Bool -> Char) -> Parser Char

Open BrianHicks opened this issue 8 years ago • 0 comments

TL;DR: I need something like keepOne : (Bool -> Char) -> Parser Char.

Context: I'm parsing commonmark markdown. The spec for thematic breaks specifies that while --- and the like are valid thematic breaks, a mix of characters is not. So I need to disallow *-* and other mixes of valid thematic break characters. I do this now by doing something like this:

initial : Parser Char
initial =
  keep (Exactly 1) anyBreakChar
    |> map (String.toList >> List.head >> Maybe.withDefault ' ')

This gives me the behavior I want, but allocates a string, a list, and a maybe (plus maybe some more stuff.) This is not terrible, but it's a big dance and results in unclear code (throwing the ' ' away, for example.)

BrianHicks avatar Aug 14 '17 14:08 BrianHicks