Skip to content

Commit 5e8ebc2

Browse files
committed
Add Zip::any
1 parent 0740695 commit 5e8ebc2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/zip/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,33 @@ macro_rules! map_impl {
669669
}).is_done()
670670
}
671671

672+
/// Tests if at least one element of the iterator matches a predicate.
673+
///
674+
/// Returns `true` if `predicate` evaluates to `true` for at least one element.
675+
/// Returns `false` if the input arrays are empty.
676+
///
677+
/// Example:
678+
///
679+
/// ```
680+
/// use ndarray::{array, Zip};
681+
/// let a = array![1, 2, 3];
682+
/// let b = array![1, 4, 9];
683+
/// assert!(Zip::from(&a).and(&b).any(|&a, &b| a == b));
684+
/// assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
685+
/// ```
686+
pub fn any<F>(mut self, mut predicate: F) -> bool
687+
where F: FnMut($($p::Item),*) -> bool
688+
{
689+
self.for_each_core((), move |_, args| {
690+
let ($($p,)*) = args;
691+
if predicate($($p),*) {
692+
FoldWhile::Done(())
693+
} else {
694+
FoldWhile::Continue(())
695+
}
696+
}).is_done()
697+
}
698+
672699
expand_if!(@bool [$notlast]
673700

674701
/// Include the producer `p` in the Zip.

0 commit comments

Comments
 (0)