Expand description
This crate gives Streams a reduce
function that is similar to
fold
but without an initial value. The function returns a Future
containing None
if the stream is empty and Some(value)
otherwise.
Based on David Tolnay’s reduce
crate for iterators.
§Examples
use stream_reduce::Reduce;
use futures::stream;
async {
// Reduce a non-empty stream into Some(value)
let v = vec![1usize, 2, 3, 4, 5];
let sum = stream::iter(v).reduce(|a, b| async move { a + b }).await;
assert_eq!(Some(15), sum);
// Reduce an empty stream into None
let v = Vec::<usize>::new();
let product = stream::iter(v).reduce(|a, b| async move { a * b }).await;
assert_eq!(None, product);
}