Is there something similar to Ruby\'s Enumerable#each_slice in Swift?
Ruby example:
arr = [\"a\", \"b\", \"c\", \"d\"]
arr.each_slice(2) {|s1, s2| puts s
Swift 4.1
extension Array {
/*
[1,2,3,4,5].forEachSlice(2, { print($0) })
=> [1, 2]
=> [3, 4]
=> [5]
*/
public func forEachSlice(_ n: Int, _ body: (ArraySlice) throws -> Void) rethrows {
assert(n > 0, "n require to be greater than 0")
for from in stride(from: self.startIndex, to: self.endIndex, by: n) {
let to = Swift.min(from + n, self.endIndex)
try body(self[from..