I have large list of objects and I need to split them in a group of two elements for UI propouse.
Example:
[0, 1, 2, 3, 4, 5, 6]
Becomes
Alternatively, you can use reduce
for this, but this is probably not the most efficient:
let res = a.reduce([[Int]]()) { (var acc: [[Int]], current: Int) in
if acc.last != nil && acc.last?.count < 2 {
var newLast = acc.last
newLast?.append(current)
acc.removeLast()
acc.append(newLast!)
} else {
acc.append([current])
}
return acc
}