I need to filter an array by some condition on its elements, but I need to obtain the indices of the elements that passed the test, not the elements themselves.
For example: given an array of Bool, I want to transform that into an array of Int that contains only the indices of the elements of the orginal array that are true.
I could do this:
// INPUT array:
let flags = [true, false, true, false]
// OUTPUT array:
var trueIndices = [Int]()
for (index, value) in flags.enumerated() where value == true {
trueIndices.append(index)
}
...but it isn't "swifty" at all.
Is there a more elegant way? Something akin to filter(), but that returns the indices instead of the elements.