forked from TimelyDataflow/timely-dataflow
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update more core operators (TimelyDataflow#555)
* Update Map * Update Filter * Update OkErr * Update Input * Update UnorderedInput * Update ToStream
- Loading branch information
1 parent
258e3af
commit b407978
Showing
13 changed files
with
828 additions
and
742 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Filters a stream by a predicate. | ||
use timely_container::{Container, PushContainer, PushInto}; | ||
|
||
use crate::dataflow::channels::pact::Pipeline; | ||
use crate::dataflow::{Scope, StreamCore}; | ||
use crate::dataflow::operators::generic::operator::Operator; | ||
|
||
/// Extension trait for filtering. | ||
pub trait Filter<C: Container> { | ||
/// Returns a new instance of `self` containing only records satisfying `predicate`. | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use timely::dataflow::operators::ToStream; | ||
/// use timely::dataflow::operators::core::{Filter, Inspect}; | ||
/// | ||
/// timely::example(|scope| { | ||
/// (0..10).to_stream(scope) | ||
/// .filter(|x| *x % 2 == 0) | ||
/// .inspect(|x| println!("seen: {:?}", x)); | ||
/// }); | ||
/// ``` | ||
fn filter<P: FnMut(&C::Item<'_>)->bool+'static>(&self, predicate: P) -> Self; | ||
} | ||
|
||
impl<G: Scope, C: PushContainer> Filter<C> for StreamCore<G, C> | ||
where | ||
for<'a> C::Item<'a>: PushInto<C> | ||
{ | ||
fn filter<P: FnMut(&C::Item<'_>)->bool+'static>(&self, mut predicate: P) -> StreamCore<G, C> { | ||
let mut container = Default::default(); | ||
self.unary(Pipeline, "Filter", move |_,_| move |input, output| { | ||
input.for_each(|time, data| { | ||
data.swap(&mut container); | ||
if !container.is_empty() { | ||
output.session(&time).give_iterator(container.drain().filter(&mut predicate)); | ||
} | ||
}); | ||
}) | ||
} | ||
} |
Oops, something went wrong.