Function itertools_wild::special::feedback [] [src]

pub fn feedback<T>(initial: T) -> Feedback<T> where
    T: Copy

Feed the output of an iterator pipeline back to the input. Feedback is delayed by one timestep to preserve causality, so the first input is provided by initial, and the output of that pass is used as the input for the second pass, and so on.

Every time the input is requested, it yields the last result returned by the pipeline. The pipeline can request the feedback value any number of times per cycle, including ignoring it entirely. If the pipeline doesn't request a particular input, that input is discarded, not saved for the next cycle. If the pipeline requests an input multiple times in the process of producing an output, the same input will be returned each time.

use itertools_wild::special::feedback;

let input = [1, -2, 3, -4, 5];
let result: Vec<i32> = feedback(0).feed(|feedback|
        feedback.zip(&input)
                .map(|(a, b)| a + b)
    ).collect();
assert_eq!(result, &[0, 1, -1, 2, -2, 3]);