oneOf -package:split
Randomly uses one of the given generators. The input list must be
non-empty.
oneOf cs succeeds if the current character is in the supplied
list of characters
cs. Returns the parsed character. See also
satisfy.
vowel = oneOf "aeiou"
oneOf ts succeeds if the current token is in the
supplied collection of tokens
ts. Returns the parsed token.
Note that this parser cannot automatically generate the “expected”
component of error message, so usually you should label it manually
with
label or (
<?>).
oneOf cs = satisfy (`elem` cs)
See also:
satisfy.
digit = oneOf ['0'..'9'] <?> "digit"
Performance note: prefer
satisfy when you can because
it's faster when you have only a couple of tokens to compare to:
quoteFast = satisfy (\x -> x == '\'' || x == '\"')
quoteSlow = oneOf "'\""
oneOf cs succeeds if the current character is in the supplied
list of characters
cs. Returns the parsed character. See also
satisfy.
vowel = oneOf "aeiou"
Parse the first alternative in the list that succeeds.
Match any one of the given characters
>>> match (oneOf "1a") "1"
"1"
>>> match (oneOf "2a") "1"
""
Nondeterministically choose an element from a
Foldable
collection. This can be used to emulate the style of nondeterminism
associated with programming in the list monad:
pythagoreanTriples = do
a <- oneOf [1..10]
b <- oneOf [1..10]
c <- oneOf [1..10]
guard (a^2 + b^2 == c^2)
pure (a, b, c)
Match any one of the elements in the supplied list.
>>> oneOf xs = Parser.satisfy (`Foldable.elem` xs)
When performance matters a pattern matching predicate could be more
efficient than a
Foldable datatype:
let p x =
case x of
a -> True
e -> True
_ -> False
in satisfy p
GHC may use a binary search instead of linear search in the list.
Alternatively, you can also use an array instead of list for storage
and search.
Helper to define a character class.
>>> prove $ \(c :: SChar) -> c `match` oneOf "ABCD" .<=> sAny (c .==) (map literal "ABCD")
Q.E.D.
Randomly uses one of the given generators. The input structure must be
non-empty.
λ> import qualified Faker.Address as FA
λ> let fakes = [FA.country, FA.postcode, FA.state]
λ> generate (oneof fakes)
Montana
Get any element of a list with equal probability.
Randomly uses one of the given generators. The input list must be
non-empty.
Generate a value with one of many generators
Uniformly selects a generator and shrinks towards the first one.
oneOf cs succeeds if the current character is in the supplied
list of characters cs. Returns the parsed character.
Select one of the provided alternatives given a number.
Call
inputlist() on the neovim side and ask the user for a
choice. This function returns
Nothing if the user input was
invalid or
Just the chosen element. The elements are rendered
via
Pretty.
oneOf cs succeeds if the current character is in the supplied
list of characters
cs. Returns the parsed character. See also
satisfy.
vowel = oneOf "aeiou"
Randomly uses one of the given generators. The input list must be
non-empty.