Hello. I have got some unclear error. With this function I want to combine repeated repeated into groups.
import Foundation
func countingValleys(s: String) -> Int {
let combinedSteps = s.reduce(into: [(direction: Character, count: Int)]()) {
if $0.last?.direction == $1 ?? false { $0[$0.endIndex - 1].count += 1 }
$0.append(($1, 1))
}
print(combinedSteps)
return combinedSteps.count
}
countingValleys(s: "udduddudduuuudddudud")
But there is error.
error: cannot convert value of type 'String.Element' (aka 'Character') to expected argument type '_?'
Why cant compiler infer type of $1, which should be Character? (right?)