字符串数组-字符串数目不变的多个子数组

时间:2021-11-06 16:04:59

Let's say I have this array of strings:

假设我有一个字符串数组

let Vehicles = ["Aeroplane", "Bicycle", "CarVehicle", "Lorry", "Motorbike", "Scooter", "Ship", "Train"]

What I want is this result:

我想要的是这个结果:

let resultArray = [["Aeroplane", "Bicycle", "CarVehicle", "Lorry"], ["Motorbike", "Scooter", "Ship", "Train"]]

I know I could do this by for but I want to use Higher Order functions in Swift. I mean functions like map, reduce, filter. I think it's possible to do this way and it could be better. Can anyone help me with this? Thanks

我知道我可以这样做,但是我想在Swift中使用更高阶的函数。我的意思是像map, reduce, filter这样的函数。我认为这样做是有可能的,而且可能会更好。有人能帮我一下吗?谢谢

1 个解决方案

#1


5  

A possible solution with map() and stride():

map()和stride()可能的解决方案:

let vehicles = ["Aeroplane", "Bicycle", "CarVehicle", "Lorry", "Motorbike", "Scooter", "Ship", "Train"]
let each = 4

let resultArray = map(stride(from: 0, to: vehicles.count, by: each)) {
    vehicles[$0 ..< advance($0, each, vehicles.count)]
}

println(resultArray)
// [[Aeroplane, Bicycle, CarVehicle, Lorry], [Motorbike, Scooter, Ship, Train]]

The usage of advance() in the closure guarantees that the code works even if the number of array elements is not a multiple of 4 (and the last subarray in the result will then be shorter.)

在闭包中使用advance()可以保证即使数组元素的数量不是4的倍数(并且结果中的最后一个子数组也会更短),代码仍然有效。

You can simplify it to

你可以化简它。

let resultArray = map(stride(from: 0, to: vehicles.count, by: each)) {
    vehicles[$0 ..< $0 + each]
}

if you know that the number of array elements is a multiple of 4.

如果你知道数组元素的个数是4的倍数。

Strictly speaking the elements of resultArray are not arrays but array slices. In many cases that does not matter, otherwise you can replace it by

严格地说,resultArray的元素不是数组,而是数组切片。在许多情况下,这并不重要,否则您可以用以下方法替换它

let resultArray = map(stride(from: 0, to: vehicles.count, by: each)) {
    Array(vehicles[$0 ..< advance($0, each, vehicles.count)])
}

#1


5  

A possible solution with map() and stride():

map()和stride()可能的解决方案:

let vehicles = ["Aeroplane", "Bicycle", "CarVehicle", "Lorry", "Motorbike", "Scooter", "Ship", "Train"]
let each = 4

let resultArray = map(stride(from: 0, to: vehicles.count, by: each)) {
    vehicles[$0 ..< advance($0, each, vehicles.count)]
}

println(resultArray)
// [[Aeroplane, Bicycle, CarVehicle, Lorry], [Motorbike, Scooter, Ship, Train]]

The usage of advance() in the closure guarantees that the code works even if the number of array elements is not a multiple of 4 (and the last subarray in the result will then be shorter.)

在闭包中使用advance()可以保证即使数组元素的数量不是4的倍数(并且结果中的最后一个子数组也会更短),代码仍然有效。

You can simplify it to

你可以化简它。

let resultArray = map(stride(from: 0, to: vehicles.count, by: each)) {
    vehicles[$0 ..< $0 + each]
}

if you know that the number of array elements is a multiple of 4.

如果你知道数组元素的个数是4的倍数。

Strictly speaking the elements of resultArray are not arrays but array slices. In many cases that does not matter, otherwise you can replace it by

严格地说,resultArray的元素不是数组,而是数组切片。在许多情况下,这并不重要,否则您可以用以下方法替换它

let resultArray = map(stride(from: 0, to: vehicles.count, by: each)) {
    Array(vehicles[$0 ..< advance($0, each, vehicles.count)])
}