struct Parser<A> {
let parse: (String) -> (A,String)?
}
func character(condition:@escaping (Character) -> Bool) -> Parser<Character> {
return Parser(parse: {str in
guard let char = str.first, condition(char) else {
return nil
}
return (char,String(str.dropFirst()))
})
}
In the code above, I have hard time figuring out what happens. More specific, what does
let char = str.first, condition(char)
do? Is this even a legal construct? The code compiles so it must be, but what is happening? let char = str.first
is an assignment and condition(char)
is a boolean. How can you have an assignment followed by a comma, followed by something that evaluates to a boolean.