Created
August 19, 2014 22:57
-
-
Save kourge/8e3171d432c05938ebf3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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