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?)
Lantua
2
I think you meant to do this
if $0.last?.direction == $1 {
$0[$0.endIndex - 1].count += 1
} else {
$0.append(($1, 1))
}
The problem with type checking is that comparing == returns Bool even in optional version. So you're doing Bool ?? false, not Bool? ?? false, which seems to be confusing the compiler.
if $0.last?.direction is nil, the comparison returns false anyway.
One other thing is that you probably don't wan't to append if you're grouping them.
1 Like
Thank you for solution. It works great.
Yeap, seems like my fault in fact. Gonna do more reading. Cheers.