Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Zip::any #1228

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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