Crates.io | reduce |
lib.rs | reduce |
version | 0.1.5+deprecated |
source | src |
created_at | 2016-06-19 22:29:49.327293 |
updated_at | 2022-12-19 18:35:45.423181 |
description | Fold an iterator without an initial value |
homepage | |
repository | https://github.com/dtolnay/reduce |
max_upload_size | |
id | 5428 |
size | 18,867 |
This crate gives Iterators a reduce
function that is similar to
fold
but without an initial value. The function returns None
if
the iterator is empty and Some(value)
otherwise. This matches the distinction
between reduce
and fold
in Scala.
[dependencies]
reduce = "0.1"
use reduce::Reduce;
fn main() {
// Reduce a non-empty iterator into Some(value)
let v = vec![1usize, 2, 3, 4, 5];
let sum = v.into_iter().reduce(|a, b| a + b);
assert_eq!(Some(15), sum);
// Reduce an empty iterator into None
let v = Vec::<usize>::new();
let sum = v.into_iter().reduce(|a, b| a + b);
assert_eq!(None, sum);
}