Skip to content

Instantly share code, notes, and snippets.

@joaomvfsantos
Created May 30, 2023 13:41
Show Gist options
  • Save joaomvfsantos/66d5aceb333622ed1d3f63972969c65a to your computer and use it in GitHub Desktop.
Save joaomvfsantos/66d5aceb333622ed1d3f63972969c65a to your computer and use it in GitHub Desktop.
Swift Array extension that chunks an array to sub arrays of a given size
//
// ChunkedArray.swift
//
// Created by João Santos on 25/05/2020.
// Copyright © 2020 João Santos. All rights reserved.
//
import Foundation
extension Array {
func chunked(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment