Trait ExoticIteratorExt

Source
pub trait ExoticIteratorExt: Iterator + Sized {
    // Required methods
    fn at_least<P: FnMut(&Self::Item) -> bool>(
        self,
        n: usize,
        predicate: P,
    ) -> bool;
    fn at_most<P: FnMut(&Self::Item) -> bool>(
        self,
        n: usize,
        predicate: P,
    ) -> bool;
    fn exactly_n<P: FnMut(&Self::Item) -> bool>(
        self,
        n: usize,
        predicate: P,
    ) -> bool;
    fn exactly_m_n<Pm: FnMut(&Self::Item) -> bool, Pn: FnMut(&Self::Item) -> bool>(
        self,
        m: usize,
        predicate_m: Pm,
        n: usize,
        predicate_n: Pn,
    ) -> bool;
    fn all_or_none<P: FnMut(&Self::Item) -> bool>(self, predicate: P) -> bool;
    fn perfectly_balanced<P: FnMut(&Self::Item) -> bool>(
        self,
        predicate: P,
    ) -> bool;
    fn alternate<U: IntoIterator<Item = Self::Item>>(
        self,
        other: U,
    ) -> Alternate<Self, U::IntoIter> ;
}
Expand description

Provides additional convenience methods to the Iterator trait and its implementors.

Required Methods§

Source

fn at_least<P: FnMut(&Self::Item) -> bool>(self, n: usize, predicate: P) -> bool

Consumes the iterator, counting the number of items that pass the predicate and returns true iff there were at least n passing items.

§Example
use exotic_iter::*;
let very_few_twos = vec![1, 2, 3, 4, 5, 6];
let lots_of_twos = vec![2, 3, 2, 4, 2, 5];
assert_eq!(false, very_few_twos.into_iter().at_least(3_usize, |n| *n == 2));
assert_eq!(true, lots_of_twos.into_iter().at_least(3_usize, |n| *n == 2));
Source

fn at_most<P: FnMut(&Self::Item) -> bool>(self, n: usize, predicate: P) -> bool

Consumes the iterator, counting the number of items that pass the predicate and returns true iff there were no more than n passing items.

§Example
use exotic_iter::*;
let just_enough_twos = vec![1, 1, 2, 2, 3, 3];
let too_many_twos = vec![2, 2, 2, 2, 2, 2];
assert_eq!(true, just_enough_twos.into_iter().at_most(2_usize, |n| *n == 2));
assert_eq!(false, too_many_twos.into_iter().at_most(2_usize, |n| *n == 2));
Source

fn exactly_n<P: FnMut(&Self::Item) -> bool>( self, n: usize, predicate: P, ) -> bool

Consumes the iterator, counting the number of items that pass the predicate and returns true iff there were exactly n passing items.

§Example
use exotic_iter::*;
let no_digits = "deadbeef";
let two_digits = "deadb33f";
let three_digits = "d3adb33f";
assert_eq!(false, no_digits.chars().exactly_n(2_usize, |c| c.is_ascii_digit()));
assert_eq!(true, two_digits.chars().exactly_n(2_usize, |c| c.is_ascii_digit()));
assert_eq!(false, three_digits.chars().exactly_n(2_usize, |c| c.is_ascii_digit()));
Source

fn exactly_m_n<Pm: FnMut(&Self::Item) -> bool, Pn: FnMut(&Self::Item) -> bool>( self, m: usize, predicate_m: Pm, n: usize, predicate_n: Pn, ) -> bool

Consumes the iterator, counting the number of items that pass each predicate and returns true iff there were exactly m passing items for the predicate_m, and exactly n passing items for predicate_n.

§Example
use exotic_iter::*;
let more_digits = "abc12345";
let balanced_digits = "abcd1234";
let more_letters = "abcde123";
assert_eq!(false, more_digits.chars().exactly_m_n(4_usize, |c| c.is_ascii_alphabetic(), 4_usize, |c| c.is_ascii_digit()));
assert_eq!(true, balanced_digits.chars().exactly_m_n(4_usize, |c| c.is_ascii_alphabetic(), 4_usize, |c| c.is_ascii_digit()));
assert_eq!(false, more_letters.chars().exactly_m_n(4_usize, |c| c.is_ascii_alphabetic(), 4_usize, |c| c.is_ascii_digit()));
Source

fn all_or_none<P: FnMut(&Self::Item) -> bool>(self, predicate: P) -> bool

Consumes the iterator and returns true iff either all of the items in the iterator passed the predicate, or none of them passed. Returns early on the first mixed result.

§Example
use exotic_iter::*;
let all_true = vec![true, true, true];
let some_true = vec![true, false, true];
let none_true = vec![false, false, false];
assert_eq!(true, all_true.into_iter().all_or_none(|b| *b));
assert_eq!(false, some_true.into_iter().all_or_none(|b| *b));
assert_eq!(true, none_true.into_iter().all_or_none(|b| *b));
Source

fn perfectly_balanced<P: FnMut(&Self::Item) -> bool>(self, predicate: P) -> bool

Consumes the iterator and returns true if exactly half of the items passed the predicate; as all things should be.

§Example
use exotic_iter::*;
let two_even = vec![1, 2, 3, 4];
let three_even = vec![1, 2, 4, 6];
assert_eq!(true, two_even.into_iter().perfectly_balanced(|n| *n % 2 == 0));
assert_eq!(false, three_even.into_iter().perfectly_balanced(|n| *n % 2 == 0));
Source

fn alternate<U: IntoIterator<Item = Self::Item>>( self, other: U, ) -> Alternate<Self, U::IntoIter>

Creates an iterator that alternates between items from self and other, with self providing the first value. Short-circuits at the first None returned, even if one of the iterators still has values left.

§Example
use exotic_iter::*;
let odd_numbers = vec![1, 3];
let even_numbers = vec![2, 4];
let mut iter = odd_numbers.iter().alternate(even_numbers.iter());
assert_eq!(Some(&1), iter.next());
assert_eq!(Some(&2), iter.next());
assert_eq!(Some(&3), iter.next());
assert_eq!(Some(&4), iter.next());
assert_eq!(None, iter.next());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§