Есть ли способ получить индекс массива в map
или reduce
в Swift? Я ищу что-то вроде each_with_index
в 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")
Я хотел бы избавиться от переменной odd
выше.