Last active
December 17, 2015 20:29
-
-
Save lilyball/5668099 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
// ... | |
pub trait VecUtil<Elem> { | |
pub fn grouped(&self, n: uint) -> ~[&[Elem]]; | |
} | |
impl<'self, Elem> VecUtil<Elem> for &'self [Elem] { | |
fn grouped(&self, n: uint) -> ~[&'self [Elem]] { | |
if self.is_empty() { return ~[]; } | |
let mut res = vec::with_capacity((self.len()-1) / n + 1); | |
for uint::range_step(0, self.len(), n) |i| { | |
res.push(self.slice(i, i+n)); | |
} | |
res | |
} | |
} |
Author
lilyball
commented
May 29, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment