Skip to content

Commit

Permalink
Add Zip::any
Browse files Browse the repository at this point in the history
  • Loading branch information
nilgoyette authored and bluss committed Mar 10, 2024
1 parent cd0a956 commit d517bef
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/zip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,33 @@ macro_rules! map_impl {
}).is_done()
}

/// Tests if at least one element of the iterator matches a predicate.
///
/// Returns `true` if `predicate` evaluates to `true` for at least one element.
/// Returns `false` if the input arrays are empty.
///
/// Example:
///
/// ```
/// use ndarray::{array, Zip};
/// let a = array![1, 2, 3];
/// let b = array![1, 4, 9];
/// assert!(Zip::from(&a).and(&b).any(|&a, &b| a == b));
/// assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
/// ```
pub fn any<F>(mut self, mut predicate: F) -> bool
where F: FnMut($($p::Item),*) -> bool
{
self.for_each_core((), move |_, args| {
let ($($p,)*) = args;
if predicate($($p),*) {
FoldWhile::Done(())
} else {
FoldWhile::Continue(())
}
}).is_done()
}

expand_if!(@bool [$notlast]

/// Include the producer `p` in the Zip.
Expand Down

0 comments on commit d517bef

Please sign in to comment.