The book Swift Coding Challenges has the following problem:
"Write a function that accepts a string, and returns how many times a specific character appears, taking case into account."
In one of the solutions
func challenge5b(input: String, count: Character) -> Int {
return input.reduce(0) {
$1 == count ? $0 + 1 : $0
}
}
It's not clear to me where$0
and $1
come from. How exactly does the reduce function work when stepping through the input string?