@@ -56,7 +56,7 @@ pub use either::Either;
56
56
57
57
#[ cfg( feature = "use_std" ) ]
58
58
use std:: collections:: HashMap ;
59
- use std:: iter:: { IntoIterator } ;
59
+ use std:: iter:: { IntoIterator , once } ;
60
60
use std:: cmp:: Ordering ;
61
61
use std:: fmt;
62
62
#[ cfg( feature = "use_std" ) ]
@@ -1928,6 +1928,64 @@ pub trait Itertools : Iterator {
1928
1928
FoldWhile :: Continue ( acc)
1929
1929
}
1930
1930
1931
+ /// Iterate over the entire iterator and add all the elements.
1932
+ ///
1933
+ /// An empty iterator returns `None`, otherwise `Some(sum)`.
1934
+ ///
1935
+ /// # Panics
1936
+ ///
1937
+ /// When calling `sum1()` and a primitive integer type is being returned, this
1938
+ /// method will panic if the computation overflows and debug assertions are
1939
+ /// enabled.
1940
+ ///
1941
+ /// # Examples
1942
+ ///
1943
+ /// ```
1944
+ /// use itertools::Itertools;
1945
+ ///
1946
+ /// let empty_sum = (1..1).sum1::<i32>();
1947
+ /// assert_eq!(empty_sum, None);
1948
+ ///
1949
+ /// let nonempty_sum = (1..11).sum1::<i32>();
1950
+ /// assert_eq!(nonempty_sum, Some(55));
1951
+ /// ```
1952
+ fn sum1 < S > ( mut self ) -> Option < S >
1953
+ where Self : Sized ,
1954
+ S : std:: iter:: Sum < Self :: Item > ,
1955
+ {
1956
+ self . next ( )
1957
+ . map ( |first| once ( first) . chain ( self ) . sum ( ) )
1958
+ }
1959
+
1960
+ /// Iterate over the entire iterator and multiply all the elements.
1961
+ ///
1962
+ /// An empty iterator returns `None`, otherwise `Some(product)`.
1963
+ ///
1964
+ /// # Panics
1965
+ ///
1966
+ /// When calling `product1()` and a primitive integer type is being returned,
1967
+ /// method will panic if the computation overflows and debug assertions are
1968
+ /// enabled.
1969
+ ///
1970
+ /// # Examples
1971
+ /// ```
1972
+ /// use itertools::Itertools;
1973
+ ///
1974
+ /// let empty_product = (1..1).product1::<i32>();
1975
+ /// assert_eq!(empty_product, None);
1976
+ ///
1977
+ /// let nonempty_product = (1..11).product1::<i32>();
1978
+ /// assert_eq!(nonempty_product, Some(3628800));
1979
+ /// ```
1980
+ fn product1 < P > ( mut self ) -> Option < P >
1981
+ where Self : Sized ,
1982
+ P : std:: iter:: Product < Self :: Item > ,
1983
+ {
1984
+ self . next ( )
1985
+ . map ( |first| once ( first) . chain ( self ) . product ( ) )
1986
+ }
1987
+
1988
+
1931
1989
/// Sort all iterator elements into a new iterator in ascending order.
1932
1990
///
1933
1991
/// **Note:** This consumes the entire iterator, uses the
0 commit comments