[][src]Trait kai::KaiIterator

pub trait KaiIterator: IntoIterator + Sized {
    fn chain_if<I, F>(self, condition: bool, f: F) -> ChainIf<Self, I>
    where
        I: IntoIterator<Item = Self::Item>,
        F: FnOnce() -> I
, { ... }
fn chain_if_else<J, K, F, G>(
        self,
        condition: bool,
        f: F,
        g: G
    ) -> ChainIfElse<Self, J, K>
    where
        J: IntoIterator<Item = Self::Item>,
        K: IntoIterator<Item = Self::Item>,
        F: FnOnce() -> J,
        G: FnOnce() -> K
, { ... } }

Generates my custom iterator adapters

For convenience, this is implmented for all types that implement not just Iterator, but IntoIterator as well.

See methods for usage.

Provided methods

Important traits for ChainIf<I, J>
fn chain_if<I, F>(self, condition: bool, f: F) -> ChainIf<Self, I> where
    I: IntoIterator<Item = Self::Item>,
    F: FnOnce() -> I, 

Chain this iterator with another if the condition is true

Example

use kai::*;

let condition = true;

// Turn this
let mut v = vec![1, 2, 3];
if condition {
    v.extend(vec![4, 5, 6])
}

// Into this
let w: Vec<_> = vec![1, 2, 3].chain_if(condition, || vec![4, 5, 6]).collect();

assert_eq!(v, w);

Important traits for ChainIfElse<I, J, K>
fn chain_if_else<J, K, F, G>(
    self,
    condition: bool,
    f: F,
    g: G
) -> ChainIfElse<Self, J, K> where
    J: IntoIterator<Item = Self::Item>,
    K: IntoIterator<Item = Self::Item>,
    F: FnOnce() -> J,
    G: FnOnce() -> K, 

Chain this iterator with one of two options based on the condition

If the condition is true, the iterator generated by f is used. If the condition is false, the iterator generated by g is used.

Example

use kai::*;

let v = vec![1, 2, 3];
let len = v.len();
let w: Vec<_> = v.into_iter().chain_if_else(
    len == 3,
    || vec![4, 5],
    || Some(6)
).collect();

assert_eq!(
    w,
    vec![1, 2, 3, 4, 5],
);
Loading content...

Implementors

impl<I> KaiIterator for I where
    I: IntoIterator + Sized
[src]

Important traits for ChainIf<I, J>
fn chain_if<I, F>(self, condition: bool, f: F) -> ChainIf<Self, I> where
    I: IntoIterator<Item = Self::Item>,
    F: FnOnce() -> I, 
[src]

Important traits for ChainIfElse<I, J, K>
fn chain_if_else<J, K, F, G>(
    self,
    condition: bool,
    f: F,
    g: G
) -> ChainIfElse<Self, J, K> where
    J: IntoIterator<Item = Self::Item>,
    K: IntoIterator<Item = Self::Item>,
    F: FnOnce() -> J,
    G: FnOnce() -> K, 
[src]

Loading content...