vowel = oneOf "aeiou"
>>> split (oneOf ",;") "hi;there,world" ["hi",";","there",",","world"]
>>> split (oneOf "xyz") "aazbxyzcxd" ["aa","z","b","x","","y","","z","c","x","d"]
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 "'\""
vowel = oneOf "aeiou"
>>> match (oneOf "1a") "1" "1" >>> match (oneOf "2a") "1" ""
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)
>>> 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 pGHC 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.
>>> prove $ \(c :: SChar) -> c `match` oneOf "ABCD" .<=> sAny (c .==) (map literal "ABCD") Q.E.D.
λ> import qualified Faker.Address as FA λ> let fakes = [FA.country, FA.postcode, FA.state] λ> generate (oneof fakes) Montana
vowel = oneOf "aeiou"