Skip to content

Commit 22a1c74

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 b07b0ad commit 22a1c74

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/lib.rs

+47-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,50 @@ pub trait Itertools: Iterator {
22232220
self.collect()
22242221
}
22252222

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

0 commit comments

Comments
 (0)