1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! Filters a stream by a predicate.

use Data;
use dataflow::channels::pact::Pipeline;
use dataflow::{Stream, Scope};
use dataflow::operators::unary::Unary;

/// Extension trait for filtering.
pub trait Filter<D: Data> {
    /// Returns a new instance of `self` containing only records satisfying `predicate`.
    ///
    /// #Examples
    /// ```
    /// use timely::dataflow::operators::{ToStream, Filter, Inspect};
    ///
    /// timely::example(|scope| {
    ///     (0..10).to_stream(scope)
    ///            .filter(|x| *x % 2 == 0)
    ///            .inspect(|x| println!("seen: {:?}", x));
    /// });
    /// ```
    fn filter<L: Fn(&D)->bool+'static>(&self, predicate: L) -> Self;
}

impl<G: Scope, D: Data> Filter<D> for Stream<G, D> {
    fn filter<L: Fn(&D)->bool+'static>(&self, predicate: L) -> Stream<G, D> {
        self.unary_stream(Pipeline, "Filter", move |input, output| {
            while let Some((time, data)) = input.next() {
                data.retain(|x| predicate(x));
                if data.len() > 0 {
                    output.session(&time).give_content(data);
                }
            }
        })
    }
}