While testing my generic 2D matrix transposition function, I ran into some strange behaviour for ranges.
extension Collection where Self.Iterator.Element: RandomAccessCollection {
func transposed() -> [[Self.Iterator.Element.Iterator.Element]] {
guard let firstRow = self.first else { return [] }
return firstRow.indices.map { index in
self.map{ $0[index] }
}
}
}
The algorithm works for normal arrays:
[[1, 2], [3, 4]].transposed().forEach{ print($0) }
But gave quite surprising results for ranges (I would have expected it to behave the same):
[1...2, 3...4].transposed().forEach{ print($0) }
// Output:
// [1, 1]
// [2, 2]
...???
Narrowing it down, it seems that the indices of ranges are not 0 indexed, and that the index you use from one range, when applied to another range, gives the same result (even if it's not found within that second range). Ex:
(1...3).indices.map{ index in (0...0)[index] } // => [1, 2, 3] π€
Could somebody explain why this behaviour occurs, what motivated the design, and how I could fix my algorithm?