@@ -33,7 +33,7 @@ pub use either::Either;
33
33
34
34
#[ cfg( feature = "use_std" ) ]
35
35
use std:: collections:: HashMap ;
36
- use std:: iter:: { IntoIterator } ;
36
+ use std:: iter:: { IntoIterator , once } ;
37
37
use std:: cmp:: Ordering ;
38
38
use std:: fmt;
39
39
#[ cfg( feature = "use_std" ) ]
@@ -1762,6 +1762,64 @@ pub trait Itertools : Iterator {
1762
1762
FoldWhile :: Continue ( acc)
1763
1763
}
1764
1764
1765
+ /// Iterates over the entire iterator, adding all the elements
1766
+ ///
1767
+ /// An empty iterator returns `None`, otherwise `Some(sum)`.
1768
+ ///
1769
+ /// # Panics
1770
+ ///
1771
+ /// When calling `sum1()` and a primitive integer type is being returned, this
1772
+ /// method will panic if the computation overflows and debug assertions are
1773
+ /// enabled.
1774
+ ///
1775
+ /// # Examples
1776
+ ///
1777
+ /// ```
1778
+ /// use itertools::Itertools;
1779
+ ///
1780
+ /// let empty_sum = (1..1).sum1::<i32>();
1781
+ /// assert_eq!(empty_sum, None);
1782
+ ///
1783
+ /// let nonempty_sum = (1..=10).sum1::<i32>();
1784
+ /// assert_eq!(nonempty_sum, Some(55));
1785
+ /// ```
1786
+ fn sum1 < S > ( mut self ) -> Option < S >
1787
+ where Self : Sized ,
1788
+ S : std:: iter:: Sum < Self :: Item > ,
1789
+ {
1790
+ self . next ( )
1791
+ . map ( |first| once ( first) . chain ( self ) . sum ( ) )
1792
+ }
1793
+
1794
+ /// Iterates over the entire iterator, multiplying all the elements
1795
+ ///
1796
+ /// An empty iterator returns `None`, otherwise `Some(product)`.
1797
+ ///
1798
+ /// # Panics
1799
+ ///
1800
+ /// When calling `product1()` and a primitive integer type is being returned,
1801
+ /// method will panic if the computation overflows and debug assertions are
1802
+ /// enabled.
1803
+ ///
1804
+ /// # Examples
1805
+ /// ```
1806
+ /// use itertools::Itertools;
1807
+ ///
1808
+ /// let empty_product = (1..1).product1::<i32>();
1809
+ /// assert_eq!(empty_product, None);
1810
+ ///
1811
+ /// let nonempty_product = (1..=10).product1::<i32>();
1812
+ /// assert_eq!(nonempty_product, Some(3628800));
1813
+ /// ```
1814
+ fn product1 < P > ( mut self ) -> Option < P >
1815
+ where Self : Sized ,
1816
+ P : std:: iter:: Product < Self :: Item > ,
1817
+ {
1818
+ self . next ( )
1819
+ . map ( |first| once ( first) . chain ( self ) . product ( ) )
1820
+ }
1821
+
1822
+
1765
1823
/// Collect all iterator elements into a sorted vector in ascending order.
1766
1824
///
1767
1825
/// **Note:** This consumes the entire iterator, uses the
@@ -1888,13 +1946,13 @@ pub trait Itertools : Iterator {
1888
1946
1889
1947
/// Return a `HashMap` of keys mapped to `Vec`s of values. Keys and values
1890
1948
/// are taken from `(Key, Value)` tuple pairs yielded by the input iterator.
1891
- ///
1949
+ ///
1892
1950
/// ```
1893
1951
/// use itertools::Itertools;
1894
- ///
1952
+ ///
1895
1953
/// let data = vec![(0, 10), (2, 12), (3, 13), (0, 20), (3, 33), (2, 42)];
1896
1954
/// let lookup = data.into_iter().into_group_map();
1897
- ///
1955
+ ///
1898
1956
/// assert_eq!(lookup[&0], vec![10, 20]);
1899
1957
/// assert_eq!(lookup.get(&1), None);
1900
1958
/// assert_eq!(lookup[&2], vec![12, 42]);
0 commit comments