Skip to content

Instantly share code, notes, and snippets.

@kourge
Created August 19, 2014 22:57
Show Gist options
  • Save kourge/8e3171d432c05938ebf3 to your computer and use it in GitHub Desktop.
Save kourge/8e3171d432c05938ebf3 to your computer and use it in GitHub Desktop.
func chunk<T : Sliceable where T.Index : Strideable>(
s: T,
#from: T.Index,
#to: T.Index,
#by: T.Index.Stride
) -> GeneratorOf<T.SubSlice> {
var g = stride(from: from, to: to, by: by).generate()
return GeneratorOf<T.SubSlice> {
if let start = g.next() {
let end = start.advancedBy(by)
var range = start ..< (end >= to ? to : end)
return s[range]
} else {
return nil
}
}
}
// let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// let c = chunk(a, from: a.startIndex, to: a.endIndex, by: 3)
// for block in c {
// println(block)
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment