2018-03-12 23:04:16 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
public extension Collection {
|
|
|
|
|
|
|
|
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
|
2020-11-16 00:34:47 +01:00
|
|
|
subscript (safe index: Index) -> Element? {
|
2018-03-12 23:04:16 +01:00
|
|
|
return indices.contains(index) ? self[index] : nil
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 03:10:28 +02:00
|
|
|
|
|
|
|
public extension Array {
|
2020-11-16 00:34:47 +01:00
|
|
|
|
2018-09-27 03:10:28 +02:00
|
|
|
func chunked(by chunkSize: Int) -> [[Element]] {
|
|
|
|
return stride(from: 0, to: self.count, by: chunkSize).map {
|
|
|
|
Array(self[$0..<Swift.min($0 + chunkSize, self.count)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|