Skip to content

Instantly share code, notes, and snippets.

@huonw
Forked from lilyball/util.rs
Last active December 17, 2015 20:38
Show Gist options
  • Select an option

  • Save huonw/5668745 to your computer and use it in GitHub Desktop.

Select an option

Save huonw/5668745 to your computer and use it in GitHub Desktop.
// ...
pub trait VecUtil<Elem> {
pub fn grouped<'r>(&'r self, n: uint) -> ~[&'r [Elem]];
}
impl<'self, Elem> VecUtil<Elem> for &'self [Elem] {
fn grouped<'r>(&'r self, n: uint) -> ~[&'r [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 as int) |i| {
res.push(self.slice(i, i+n));
}
res
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment