I want to count the individual elements in the following array:
let b = [
1, 2, 3,
[4,5,6],
[
[7,8],
[9,0]
]
]
, and I was able to count the following array:
let a = [
[1,2,3],
[4,5],
[6,7,8,9]
]
with the following code:
protocol DeepCountable {
var deepCount: Int {get}
}
// conditional conformance
extension Array: DeepCountable where Element: DeepCountable {
var deepCount: Int {
return self.reduce(0){$0 + $1.deepCount}
}
}
extension Int: DeepCountable {
var deepCount: Int { return 1 }
}
print(a.deepCount) // 9
How do I do the same thing to array b?
print( b.deepCount )