Swift 中是否有对应的flattenScala、Xtend、Groovy、Ruby 等?
flatten
var aofa = [[1,2,3],[4],[5,6,7,8,9]] aofa.flatten() // shall deliver [1,2,3,4,5,6,7,8,9]
当然我可以为此使用 reduce 但这有点糟糕
var flattened = aofa.reduce(Int[]()){ a,i in var b : Int[] = a b.extend(i) return b }
斯威夫特 >= 3.0
reduce :
reduce
let numbers = [[1,2,3],[4],[5,6,7,8,9]] let reduced = numbers.reduce([], +)
flatMap :
flatMap
let numbers = [[1,2,3],[4],[5,6,7,8,9]] let flattened = numbers.flatMap { $0 }
joined :
joined
let numbers = [[1,2,3],[4],[5,6,7,8,9]] let joined = Array(numbers.joined())