mirror of
https://github.com/nelsbrock/iter_accumulate.git
synced 2026-08-14 22:16:48 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02010fd0aa
|
||
|
|
3ce16dbc2f
|
||
|
|
0eaae32c9b
|
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "iter_accumulate"
|
name = "iter_accumulate"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
authors = ["Niklas Elsbrock <mail@nelsbrock.de>"]
|
authors = ["Niklas Elsbrock <mail@nelsbrock.de>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "An iterator adaptor that accumulates the elements and yields the current accumulated value for each iteration"
|
description = "An iterator adaptor that accumulates the elements and yields the current accumulated value for each iteration"
|
||||||
|
|||||||
+15
-17
@@ -1,30 +1,31 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
//! This crate provides [`accumulate()`], an iterator adaptor that accumulates the elements from the
|
//! This crate provides [`accumulate`], an iterator adaptor that accumulates the elements from the
|
||||||
//! base iterator using the provided closure.
|
//! base iterator using the provided closure.
|
||||||
//!
|
//!
|
||||||
//! [`accumulate()`] takes two arguments: an initial value, and a closure with two arguments:
|
//! [`accumulate`] takes two arguments: an initial value, and a closure with two arguments:
|
||||||
//! an 'accumulator', and an element.
|
//! an 'accumulator', and an element.
|
||||||
//!
|
//!
|
||||||
//! The initial value is the value the accumulator will have when the closure is first called.
|
//! The initial value is the value the accumulator will have when the closure is first called.
|
||||||
//! On each call to [`next()`], the closure is executed with the current accumulator and the element
|
//! On each call to [`Iterator::next`], the closure is executed with the current accumulator and the
|
||||||
//! yielded by the upstream iterator. The return value of the closure is then set as the new value
|
//! element yielded by the upstream iterator. The return value of the closure is then set as the new
|
||||||
//! of the accumulator and returned to the caller.
|
//! value of the accumulator and returned to the caller.
|
||||||
//!
|
//!
|
||||||
//! Since the accumulated value needs to be both stored as the accumulator *and* returned to the
|
//! Since the accumulated value needs to be both stored as the accumulator *and* returned to the
|
||||||
//! caller, the accumulator type must implement [`Clone`].
|
//! caller, the accumulator type must implement [`Copy`]. If you want to operate on non-copyable
|
||||||
|
//! types, you should use [`Iterator::scan`] instead.
|
||||||
//!
|
//!
|
||||||
//! The returned iterator is **not** fused and it is not specified what happens when the base
|
//! The returned iterator is **not** fused and it is not specified what happens when the base
|
||||||
//! iterator returns [`None`].
|
//! iterator returns [`None`].
|
||||||
//! If you want a fused iterator, use [`fuse()`].
|
//! If you want a fused iterator, use [`Iterator::fuse`].
|
||||||
//!
|
//!
|
||||||
//! # Differences to [`fold()`]
|
//! # Differences to [`Iterator::fold`]
|
||||||
//!
|
//!
|
||||||
//! In principle, [`accumulate()`] is similar to [`fold()`]. However, instead of returning the final
|
//! In principle, [`accumulate`] is similar to [`Iterator::fold`]. However, instead of returning
|
||||||
//! accumulated result, it returns an iterator that yields the current value of the accumulator for
|
//! the final accumulated result, it returns an iterator that yields the current value of the
|
||||||
//! each iteration. In other words, the last element yielded by [`accumulate()`] is what would have
|
//! accumulator for each iteration. In other words, the last element yielded by [`accumulate`] is
|
||||||
//! been returned by [`fold()`] if it had been used instead.
|
//! what would have been returned by [`Iterator::fold`] if it had been used instead.
|
||||||
//!
|
//!
|
||||||
//! # Examples
|
//! # Examples
|
||||||
//!
|
//!
|
||||||
@@ -42,10 +43,7 @@
|
|||||||
//! assert_eq!(iter.next(), None);
|
//! assert_eq!(iter.next(), None);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! [`accumulate()`]: IterAccumulate::accumulate
|
//! [`accumulate`]: IterAccumulate::accumulate
|
||||||
//! [`fold()`]: Iterator::fold
|
|
||||||
//! [`next()`]: Iterator::next
|
|
||||||
//! [`fuse()`]: Iterator::fuse
|
|
||||||
|
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
@@ -110,7 +108,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An [`Iterator`] blanket implementation that provides the [`accumulate()`](Self::accumulate)
|
/// An [`Iterator`] blanket implementation that provides the [`accumulate`](Self::accumulate)
|
||||||
/// function.
|
/// function.
|
||||||
pub trait IterAccumulate: Iterator {
|
pub trait IterAccumulate: Iterator {
|
||||||
/// Creates an iterator adaptor that accumulates the elements from the base iterator using the
|
/// Creates an iterator adaptor that accumulates the elements from the base iterator using the
|
||||||
|
|||||||
Reference in New Issue
Block a user