Trait pushgen::GeneratorExt[][src]

pub trait GeneratorExt: Sealed + Generator {
    fn chain<Gen>(self, other: Gen) -> Chain<Self, Gen>
    where
        Self: Sized,
        Gen: Generator<Output = Self::Output>
, { ... }
fn filter<Pred>(self, predicate: Pred) -> Filter<Self, Pred>
    where
        Self: Sized,
        Pred: FnMut(&Self::Output) -> bool
, { ... }
fn map<Trans, Out>(self, transform_fn: Trans) -> Map<Self, Trans, Out>
    where
        Self: Sized,
        Trans: FnMut(Self::Output) -> Out
, { ... }
fn skip(self, n: usize) -> Skip<Self>
    where
        Self: Sized
, { ... }
fn take(self, n: usize) -> Take<Self>
    where
        Self: Sized
, { ... }
fn for_each<Func>(&mut self, func: Func) -> GeneratorResult
    where
        Self: Sized,
        Func: FnMut(Self::Output)
, { ... }
fn zip<Right>(self, right: Right) -> Zip<Self, Right>
    where
        Self: Sized,
        Right: Generator
, { ... } }
Expand description

Provides extension-methods for all generators.

This allows generators to be composed to new generators, or consumed.

Example

use pushgen::{SliceGenerator, GeneratorExt};
let data = [1, 2, 3, 4];
let mut output: Vec<i32> = Vec::new();
SliceGenerator::new(&data).map(|x| x*3).for_each(|x| output.push(x));
assert_eq!(output, [3,6,9,12]);

Provided methods

Creates a generator by chaining two generators, running them one after the other.

Example

use pushgen::{SliceGenerator, GeneratorExt};
let data = [1, 2, 3];
let mut output: Vec<i32> = Vec::new();
SliceGenerator::new(&data).chain(SliceGenerator::new(&data)).for_each(|x| output.push(*x));
assert_eq!(output, [1, 2, 3, 1, 2, 3]);

Create a filtered generator. Only values for which the predicate returns true will be passed on.

The predicate must implement FnMut(&Gen::Output) -> bool.

Example

let input = [1,2,3,4];
let mut output: Vec<i32> = Vec::new();
let run_result = SliceGenerator::new(&input).filter(|x| *x % 2 == 0).for_each(|x| output.push(*x));
assert_eq!(run_result, GeneratorResult::Complete);
assert_eq!(output, [2,4]);

Takes a closure and creates a generator which calls the closure on each value.

Example

use pushgen::{SliceGenerator, GeneratorExt};
let data = [1, 2, 3];
let mut output: Vec<String> = Vec::new();
SliceGenerator::new(&data).map(|x| x.to_string()).for_each(|x| output.push(x));
assert_eq!(output, ["1", "2", "3"]);

Skips over n values, consuming and ignoring them.

Example

 let input = [1,2,3,4];
 let mut skipped_generator = SliceGenerator::new(&input).skip(2);
 let mut output: Vec<i32> = Vec::new();
 skipped_generator.for_each(|x| output.push(*x));
 assert_eq!(output, [3,4]);

Takes n values and then completes the generator.

Example

use pushgen::{SliceGenerator, GeneratorExt};
let data = [1, 2, 3, 4];
let mut output: Vec<i32> = Vec::new();
SliceGenerator::new(&data).take(2).for_each(|x| output.push(*x));
assert_eq!(output, [1, 2]);

Run a generator to completion, or until it is stopped, and call a closure for each value produced by the generator.

The closure will be called for as long as the generator produces values, it is not possible to abort processing early. If early abort is needed, use Generator::run

Example

let mut sum = 0i32;
let data = [1,2,3];
let result = SliceGenerator::new(&data).for_each(|x| sum += x);
assert_eq!(sum, 6);
assert_eq!(result, GeneratorResult::Complete);

Zips the output of two generators into a single generator of pairs.

zip() returns a new generator that will use values from two generators, outputting a tuple where the first element comes from the first generator, and the second element comes from the second generator.

The zip generator will complete when either generator completes.

Example

use pushgen::{SliceGenerator, GeneratorExt};
let left = [1, 2, 3];
let right = [4, 5, 6];
let mut output: Vec<(i32, i32)> = Vec::new();
SliceGenerator::new(&left).zip(SliceGenerator::new(&right)).for_each(|(a, b)| output.push((*a, *b)));
assert_eq!(output, [(1,4), (2, 5), (3, 6)]);

Implementors