If I have an object that is declared as
如果我有一个声明为的对象
let compoundArray = [Array<String>]
is there a property that would give me the number of strings in all the arrays contained in compoundArray?
有没有一个属性可以给我在compoundArray中包含的所有数组中的字符串数?
I can do it by adding up all the items in each array within:
我可以通过在每个数组中添加所有项目来实现:
var totalCount = 0
for array in compoundArray {
totalCount += array.count }
//totalCount = total items in all arrays within compoundArray
But that seems clunky and it seems that swift would have a property/method of Array to do this, no?
但这似乎很笨拙,似乎swift会有一个Array的属性/方法来做到这一点,不是吗?
Thanks!
谢谢!
3 个解决方案
#1
8
You can add the nested array counts with
您可以使用添加嵌套数组计数
let count = compoundArray.reduce(0) { $0 + $1.count }
Performance comparison for large arrays (compiled and run on a MacBook Pro in Release configuration):
大型阵列的性能比较(在发布配置中在MacBook Pro上编译和运行):
let N = 20_000
let compoundArray = Array(repeating: Array(repeating: "String", count: N), count: N)
do {
let start = Date()
let count = compoundArray.joined().count
let end = Date()
print(end.timeIntervalSince(start))
// 0.729196012020111
}
do {
let start = Date()
let count = compoundArray.flatMap({$0}).count
let end = Date()
print(end.timeIntervalSince(start))
// 29.841913998127
}
do {
let start = Date()
let count = compoundArray.reduce(0) { $0 + $1.count }
let end = Date()
print(end.timeIntervalSince(start))
// 0.000432014465332031
}
#2
7
You can use joined
or flatMap
for that.
您可以使用join或flatMap。
Using joined
使用加入
let count = compoundArray.joined().count
Using flatMap
使用flatMap
let count = compoundArray.flatMap({$0}).count
#3
1
Since you are asking for a property, I thought I'd point out how to create one (for all collections, while we're at it):
既然你要求一个属性,我想我会指出如何创建一个属性(对于所有的集合,我们在它的时候):
extension Collection where Iterator.Element: Collection {
var flatCount: Int {
self.reduce(0) { $0 + $1.count } // as per Martin R
}
}
Making this recursive seems to be an interesting exercise.
使这种递归似乎是一个有趣的练习。
#1
8
You can add the nested array counts with
您可以使用添加嵌套数组计数
let count = compoundArray.reduce(0) { $0 + $1.count }
Performance comparison for large arrays (compiled and run on a MacBook Pro in Release configuration):
大型阵列的性能比较(在发布配置中在MacBook Pro上编译和运行):
let N = 20_000
let compoundArray = Array(repeating: Array(repeating: "String", count: N), count: N)
do {
let start = Date()
let count = compoundArray.joined().count
let end = Date()
print(end.timeIntervalSince(start))
// 0.729196012020111
}
do {
let start = Date()
let count = compoundArray.flatMap({$0}).count
let end = Date()
print(end.timeIntervalSince(start))
// 29.841913998127
}
do {
let start = Date()
let count = compoundArray.reduce(0) { $0 + $1.count }
let end = Date()
print(end.timeIntervalSince(start))
// 0.000432014465332031
}
#2
7
You can use joined
or flatMap
for that.
您可以使用join或flatMap。
Using joined
使用加入
let count = compoundArray.joined().count
Using flatMap
使用flatMap
let count = compoundArray.flatMap({$0}).count
#3
1
Since you are asking for a property, I thought I'd point out how to create one (for all collections, while we're at it):
既然你要求一个属性,我想我会指出如何创建一个属性(对于所有的集合,我们在它的时候):
extension Collection where Iterator.Element: Collection {
var flatCount: Int {
self.reduce(0) { $0 + $1.count } // as per Martin R
}
}
Making this recursive seems to be an interesting exercise.
使这种递归似乎是一个有趣的练习。