pub fn flatten<T: 'static, S, R: 'static>(source: S) -> Source<T> where
S: Into<Arc<Source<R>>>,
R: Into<Arc<Source<T>>>,
Expand description
Callbag operator that flattens a higher-order callbag source.
Like ReactiveX switch
or xstream flatten
. Use it
with map
to get behavior equivalent to RxJS switchMap
.
Works on either pullable or listenable sources.
Examples
Listenables
Pullables
Loop over two iterables (such as arrays) and combine their values together:
use crossbeam_queue::SegQueue;
use std::sync::Arc;
use callbag::{flatten, for_each, from_iter, map, pipe};
let actual = Arc::new(SegQueue::new());
let source = pipe!(
from_iter("hi".chars()),
map(|r#char| pipe!(
from_iter([10, 20, 30]),
map(move |num| format!("{}{}", r#char, num)),
)),
flatten,
for_each({
let actual = Arc::clone(&actual);
move |x: String| {
println!("{}", x);
actual.push(x.clone());
}
}),
);
assert_eq!(
&{
let mut v = vec![];
for _i in 0..actual.len() {
v.push(actual.pop().unwrap());
}
v
}[..],
["h10", "h20", "h30", "i10", "i20", "i30"]
);