-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(corelib): Iterator::product (#7156)
- Loading branch information
1 parent
d3da51b
commit 11b20fc
Showing
5 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod accum; | ||
mod collect; | ||
mod iterator; | ||
pub use accum::Product; | ||
pub use collect::{FromIterator, IntoIterator}; | ||
pub use iterator::Iterator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// Trait to represent types that can be created by multiplying elements of an | ||
/// iterator. | ||
/// | ||
/// This trait is used to implement [`Iterator::product()`]. Types which implement | ||
/// this trait can be generated by using the [`product()`] method on an iterator. | ||
/// Like [`FromIterator`], this trait should rarely be called directly. | ||
/// | ||
/// [`product()`]: Iterator::product | ||
/// [`FromIterator`]: crate::iter::FromIterator | ||
pub trait Product<A> { | ||
/// Takes an iterator and generates `Self` from the elements by "summing up" | ||
/// the items. | ||
fn product<I, +Iterator<I>[Item: A], +Destruct<I>, +Destruct<A>>(iter: I) -> A; | ||
} | ||
|
||
impl ProductMultiplicativeTypesImpl< | ||
A, +Mul<A>, impl OneA: core::num::traits::One<A>, | ||
> of Product<A> { | ||
fn product<I, +Iterator<I>[Item: A], +Destruct<I>, +Destruct<A>>(mut iter: I) -> A { | ||
iter.fold(OneA::one(), |acc, x| acc * x) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters