[][src]Trait exotic_iter::ExoticIteratorExt

pub trait ExoticIteratorExt: Iterator {
    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; }

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

Required methods

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));

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));

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().any_n(2_usize, |c| c.is_ascii_digit()));
assert_eq!(true, two_digits.chars().any_n(2_usize, |c| c.is_ascii_digit()));
assert_eq!(false, three_digits.chars().any_n(2_usize, |c| c.is_ascii_digit()));

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 n 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().any_m_n(4_usize, |c| c.is_ascii_alphabetic(), 4_usize, |c| c.is_ascii_digit()));
assert_eq!(true, balanced_digits.chars().any_m_n(4_usize, |c| c.is_ascii_alphabetic(), 4_usize, |c| c.is_ascii_digit()));
assert_eq!(false, more_letters.chars().any_m_n(4_usize, |c| c.is_ascii_alphabetic(), 4_usize, |c| c.is_ascii_digit()));

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));

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));
Loading content...

Implementors

impl<T: Iterator> ExoticIteratorExt for T[src]

Loading content...