If I want to enumerate an array (say for a map()
function where I would need to use the index of an element together with its value), I could use enumerate()
function. E.g.:
import Foundation
let array: [Double] = [1, 2, 3, 4]
let powersArray = array.enumerate().map() {
pow($0.element, Double($0.index))
}
print("array == \(array)")
print("powersArray == \(powersArray)")
// array == [1.0, 2.0, 3.0, 4.0]
// powersArray == [1.0, 2.0, 9.0, 64.0] <- As expected
Now, if I want to use some sub-sequence from the array, I could use a slice
, and it would allow me to use just the same indices as I would use in the original array (which is just what I want in case if I would use subscript
accessor in a for
loop). E.g.:
let range = 1..<(array.count - 1)
let slice = array[range]
var powersSlice = [Double]()
for index in slice.indices {
powersSlice.append(pow(slice[index], Double(index)))
}
print("powersSlice == \(powersSlice)")
// powersSlice == [2.0, 9.0] <- As expected
However, should I try to use enumerate().map()
approach like I did with original array, then I get totally different behaviour. Instead of slice
's range of indices I would get a new, 0-based range:
let powersSliceEnumerate = slice.enumerate().map() {
pow($0.element, Double($0.index))
}
print("powersSliceEnumerate == \(powersSliceEnumerate)")
// powersSliceEnumerate == [1.0, 3.0] <- Not as expected
Question is whether there is a decent way (i.e. without manual adjustments using offsets, or something) to enumerate a slice using its own indices and not the auto-generated 0-based ones?