Last active
February 28, 2016 23:23
-
-
Save robertmryan/5ca3ef29330086e25675 to your computer and use it in GitHub Desktop.
Range rendition of `splitEvery` in Swift 2
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
extension Range { | |
/// Split range into an array of ranges of predetermined size. | |
/// | |
/// Adapted from http://stackoverflow.com/a/26691258/1271826 | |
/// | |
/// - parameter every: How many items per subrange | |
/// - returns: An array of Range objects | |
func splitEvery(every: Element.Distance) -> [Range] { | |
var result = [Range]() | |
var from = startIndex | |
while from != endIndex { | |
let to = from.advancedBy(every, limit: endIndex) | |
result.append(from ..< to) | |
from = to | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a follow-up to Martin's post.