Map or reduce with index in Swift

Jonas Elfström picture Jonas Elfström · Jan 18, 2015 · Viewed 56.5k times · Source

Is there a way to get the index of the array in map or reduce in Swift? I'm looking for something like each_with_index in Ruby.

func lunhCheck(number : String) -> Bool
{
    var odd = true;
    return reverse(number).map { String($0).toInt()! }.reduce(0) {
        odd = !odd
        return $0 + (odd ? ($1 == 9 ? 9 : ($1 * 2) % 9) : $1)
    }  % 10 == 0
}

lunhCheck("49927398716")
lunhCheck("49927398717")

I would like to get rid of the odd variable above.

Answer

Nate Cook picture Nate Cook · Jan 18, 2015

You can use enumerate to convert a sequence (Array, String, etc.) to a sequence of tuples with an integer counter and and element paired together. That is:

let numbers = [7, 8, 9, 10]
let indexAndNum: [String] = numbers.enumerate().map { (index, element) in
    return "\(index): \(element)"
}
print(indexAndNum)
// ["0: 7", "1: 8", "2: 9", "3: 10"]

Link to enumerate definition

Note that this isn't the same as getting the index of the collection—enumerate gives you back an integer counter. This is the same as the index for an array, but on a string or dictionary won't be very useful. To get the actual index along with each element, you can use zip:

let actualIndexAndNum: [String] = zip(numbers.indices, numbers).map { "\($0): \($1)" }
print(actualIndexAndNum)
// ["0: 7", "1: 8", "2: 9", "3: 10"]

When using an enumerated sequence with reduce, you won't be able to separate the index and element in a tuple, since you already have the accumulating/current tuple in the method signature. Instead, you'll need to use .0 and .1 on the second parameter to your reduce closure:

let summedProducts = numbers.enumerate().reduce(0) { (accumulate, current) in
    return accumulate + current.0 * current.1
    //                          ^           ^
    //                        index      element
}
print(summedProducts)   // 56

Swift 3.0 and above

Since Swift 3.0 syntax is quite different.
Also, you can use short-syntax/inline to map array on dictionary:

let numbers = [7, 8, 9, 10]
let array: [(Int, Int)] = numbers.enumerated().map { ($0, $1) }
//                                                     ^   ^
//                                                   index element

That produces:

[(0, 7), (1, 8), (2, 9), (3, 10)]