pub fn map<I: 'static, O: 'static, F: 'static, S>(
f: F
) -> Box<dyn Fn(S) -> Source<O>> where
F: Fn(I) -> O + Send + Sync + Clone,
S: Into<Arc<Source<I>>>,
Expand description
Callbag operator that applies a transformation on data passing through it.
Works on either pullable or listenable sources.
Examples
use crossbeam_queue::SegQueue;
use std::sync::Arc;
use callbag::{for_each, from_iter, map};
let actual = Arc::new(SegQueue::new());
let source = map(|x| (x as f64 * 0.1) as usize)(from_iter([10, 20, 30, 40]));
for_each({
let actual = Arc::clone(&actual);
move |x| {
println!("{}", x);
actual.push(x);
}
})(source);
assert_eq!(
&{
let mut v = vec![];
for _i in 0..actual.len() {
v.push(actual.pop().unwrap());
}
v
}[..],
[1, 2, 3, 4]
);