Skip to content

Commit f46851c

Browse files
committed
Add try_product and try_sum
`.try_product()` is a more convenient way of writing `.product::<Result<_, _>>()` `.try_sum()` is a more convenient way of writing `.sum::<Result<_, _>>()`
1 parent 0fc4675 commit f46851c

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/lib.rs

+53-6
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ extern crate alloc;
5252

5353
#[cfg(feature = "use_alloc")]
5454
use alloc::{string::String, vec::Vec};
55-
56-
pub use either::Either;
57-
5855
use core::borrow::Borrow;
56+
pub use either::Either;
5957
use std::cmp::Ordering;
6058
#[cfg(feature = "use_std")]
6159
use std::collections::HashMap;
@@ -144,8 +142,7 @@ pub mod traits {
144142

145143
pub use crate::concat_impl::concat;
146144
pub use crate::cons_tuples_impl::cons_tuples;
147-
pub use crate::diff::diff_with;
148-
pub use crate::diff::Diff;
145+
pub use crate::diff::{diff_with, Diff};
149146
#[cfg(feature = "use_alloc")]
150147
pub use crate::kmerge_impl::kmerge_by;
151148
pub use crate::minmax::MinMaxResult;
@@ -2195,7 +2192,7 @@ pub trait Itertools: Iterator {
21952192
self.collect()
21962193
}
21972194

2198-
/// `.try_collect()` is more convenient way of writing
2195+
/// `.try_collect()` is a more convenient way of writing
21992196
/// `.collect::<Result<_, _>>()`
22002197
///
22012198
/// # Example
@@ -2223,6 +2220,56 @@ pub trait Itertools: Iterator {
22232220
self.collect()
22242221
}
22252222

2223+
/// `.product_ok()` is a more convenient way of writing `.product::<Result<_, _>>()`
2224+
///
2225+
/// **Panics** when a primitive integer type is returned and the computation
2226+
/// overflows, and debug assertions are enabled.
2227+
///
2228+
/// # Example
2229+
///
2230+
/// ```
2231+
/// use itertools::Itertools;
2232+
/// use std::str::FromStr;
2233+
///
2234+
/// fn main() -> Result<(), std::num::ParseIntError> {
2235+
/// let product: u64 = ["1", "2", "3"].iter().map(|x| u64::from_str(x)).product_ok()?;
2236+
/// assert_eq!(product, 6);
2237+
/// Ok(())
2238+
/// }
2239+
/// ```
2240+
fn product_ok<T, U, E>(self) -> Result<U, E>
2241+
where
2242+
Self: Sized + Iterator<Item = Result<T, E>>,
2243+
Result<U, E>: std::iter::Product<Result<T, E>>,
2244+
{
2245+
self.product()
2246+
}
2247+
2248+
/// `.sum_ok()` is a more convenient way of writing `.sum::<Result<_, _>>()`
2249+
///
2250+
/// **Panics** when a primitive integer type is returned and the computation
2251+
/// overflows, and debug assertions are enabled.
2252+
///
2253+
/// # Example
2254+
///
2255+
/// ```
2256+
/// use itertools::Itertools;
2257+
/// use std::str::FromStr;
2258+
///
2259+
/// fn main() -> Result<(), std::num::ParseIntError> {
2260+
/// let sum: u64 = ["1", "2", "3"].iter().map(|x| u64::from_str(x)).sum_ok()?;
2261+
/// assert_eq!(sum, 6);
2262+
/// Ok(())
2263+
/// }
2264+
/// ```
2265+
fn sum_ok<T, U, E>(self) -> Result<U, E>
2266+
where
2267+
Self: Sized + Iterator<Item = Result<T, E>>,
2268+
Result<U, E>: std::iter::Sum<Result<T, E>>,
2269+
{
2270+
self.sum()
2271+
}
2272+
22262273
/// Assign to each reference in `self` from the `from` iterator,
22272274
/// stopping at the shortest of the two iterators.
22282275
///

0 commit comments

Comments
 (0)