-
-
Save alemar11/ab286bf6fe4a164f2261 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://blog.krzyzanowskim.com | |
import Cocoa | |
struct ChunkSequence<Element>: SequenceType { | |
let chunkSize: Array<Element>.Index | |
let collection: Array<Element> | |
func generate() -> AnyGenerator<ArraySlice<Element>> { | |
var offset:Array<Element>.Index = collection.startIndex | |
return anyGenerator { | |
let result = self.collection[offset..<offset.advancedBy(self.chunkSize, limit: self.collection.endIndex)] | |
offset += result.count | |
return result.count > 0 ? result : nil | |
} | |
} | |
} | |
extension Array { | |
func slice(every every: Index) -> ChunkSequence<Element> { | |
return ChunkSequence(chunkSize: every, collection: self) | |
} | |
} | |
var π = ["π°","π°","π°","π°","π°"] | |
for π° in π.slice(every: 2) { | |
print(π°) | |
} | |
var array:Array<Int> = [1,2,3,4,5] | |
for slice in array.slice(every: 2) { | |
print(slice) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment