Map or reduce with index in Swift
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.