Function group

Source
pub fn group<T, G>(
    inp: impl Iterator<Item = T>,
    belong: impl Fn(&T, &T) -> bool,
    construct: impl Fn(&mut Option<Vec<T>>) -> G,
) -> impl Iterator<Item = G>
Expand description

Build groups of items from the input stream. A group finishes when belong, being passed the previous and new item, returns false. Each group is first collected in a Vec, then passed to the construct function as a mutable reference via an Option (which is always Some), and the return value becomes the item in the resulting sequence. The reason the vector is passed via Option is so that construct can take it out via .take().unwrap() if it wishes; if it does, group creates a new Vec for the next group, otherwise it reuses the old one for efficiency. The resulting iterator is empty (no group is reported) if the input is empty.