在Swift中映射或减少索引 [英] Map or reduce with index in Swift
问题描述
有没有办法在Swift中获取 map
或 reduce
的数组索引?我在Ruby中寻找类似于 each_with_index
的元素。
func lunhCheck (number:String) - > Bool
{
var odd = true;
返回反向(数字).map {String($ 0).toInt()! } .reduce(0){
odd =!odd
return $ 0 +(odd?($ 1 == 9?9:($ 1 * 2)%9):$ 1)
}% 10 == 0
}
lunhCheck(49927398716)
lunhCheck(49927398717)
我想摆脱 请注意,这与获取集合的索引不同 - 语法是完全不同的。 产生: Is there a way to get the index of the array in I would like to get rid of the You can use Note that this isn't the same as getting the index of the collection— When using an enumerated sequence with
Since Swift 3.0 syntax is quite different. That produces:
这篇关于在Swift中映射或减少索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! odd
变量 枚举将一个序列( Array
, String
等)转换为带有整数计数器和元素的元组配对在一起。那就是:
$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ numbers.enumerate()。map {(index,element)in
return\(index):\(element)
}
print(indexAndNum)
/ / [0:7,1:8,2:9,3:10]
enumerate
会返回一个整数计数器。这与数组的索引相同,但在字符串或字典上不会很有用。要获得实际的索引以及每个元素,可以使用 zip
: <$ c $打印(actualIndexAndNum)
// [] 0:7,1:8,2:9,3:10]
<当使用带有 reduce
的枚举序列时,您将无法分离元组中的索引和元素,因为您已经在元组中拥有了累加/当前元组方法签名。相反,你需要在第二个参数中使用 .0
和 .1
到 reduce
闭包:
let summedProducts = numbers.enumerate()。reduce(0){( (累计,当前)in
return accumulate + current.0 * current.1
// ^ ^
//索引元素
}
print(summedProducts)// 56
Swift 3.0
此外,您可以使用短语法/内联来映射字典上的数组:
let numbers = [7,8,9,10]
let array:[(Int,Int)] = numbers.enumerated()。map {($ 0,$ 1)}
// ^ ^
//索引元素
[(0,7),(1,8),(2,9),(3,10)]
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")
odd
variable above.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"]
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"]
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
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
[(0, 7), (1, 8), (2, 9), (3, 10)]