Im looking through examples were you can use reduce to sum an array, but mostly finding examples with numbers.
How would you add up all Bar that are inside Foo that has isAvailable set to true using reduce()?
Would you prefer to write it as I've done it? (readability & efficiency in mind)
struct Foo {
var isAvailable: Bool
var bars: [Bar]
}
struct Bar {
var name: String
}
let array = [
Foo(isAvailable: true, bars: [ Bar(name: "Bill"), Bar(name: "Donald") ]),
Foo(isAvailable: false, bars: [ Bar(name: "Barack"), Bar(name: "Joe") ]),
Foo(isAvailable: true, bars: [ Bar(name: "George"), Bar(name: "Ronald") ])
]
// Do this with reduce??
var totalCount = 0
for foo in array where foo.isAvailable {
totalCount += foo.bars.count
}