[][src]Trait tuple_combinator::TupleReducer

pub trait TupleReducer {
    fn fold<U, F: Fn(Option<U>, &dyn Any) -> Option<U>>(
        self,
        init: U,
        f: F
    ) -> Option<U>; }

Reduce tuples of Options into results of various form, act in comparable to the iterators.

use tuple_combinator::TupleReducer;

let res = (Some(1), Some(5), Some("rust_tuple")).fold(0, |sum, item| {
    sum.and_then(|s| {
        if let Some(raw_i32) = item.downcast_ref::<Option<i32>>() {
            return raw_i32.as_ref()
                .and_then(|val| {
                    Some(s + val)
                });
        }

        if let Some(raw_str) = item.downcast_ref::<Option<&str>>() {
            return raw_str.as_ref()
                .and_then(|val| {
                    Some(s + val.len() as i32)
                });
        }

        Some(s)
    })
});

assert_eq!(res, Some(16));

Required methods

fn fold<U, F: Fn(Option<U>, &dyn Any) -> Option<U>>(
    self,
    init: U,
    f: F
) -> Option<U>

Fold the tuple to obtain a final outcome. Depending on the implementation of the handler function, the fold can behave differently on various option types or values.

Examples

Reduce tuples of i32 options to the sum of the contained values:

use tuple_combinator::TupleReducer;

let res = (Some(17), Some(20)).fold(5, |sum, item| {
    sum.and_then(|s| {
        item.downcast_ref::<Option<i32>>()
            .and_then(|raw| raw.as_ref())
            .and_then(|val| {
                Some(s + val)
             })
    })
});

assert_eq!(res, Some(42));
Loading content...

Implementations on Foreign Types

impl<T0> TupleReducer for (Option<T0>,) where
    T0: 'static, 
[src]

impl<T0, T1> TupleReducer for (Option<T0>, Option<T1>) where
    T0: 'static,
    T1: 'static, 
[src]

impl<T0, T1, T2> TupleReducer for (Option<T0>, Option<T1>, Option<T2>) where
    T0: 'static,
    T1: 'static,
    T2: 'static, 
[src]

impl<T0, T1, T2, T3> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>) where
    T0: 'static,
    T1: 'static,
    T2: 'static,
    T3: 'static, 
[src]

impl<T0, T1, T2, T3, T4> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>) where
    T0: 'static,
    T1: 'static,
    T2: 'static,
    T3: 'static,
    T4: 'static, 
[src]

impl<T0, T1, T2, T3, T4, T5> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>) where
    T0: 'static,
    T1: 'static,
    T2: 'static,
    T3: 'static,
    T4: 'static,
    T5: 'static, 
[src]

impl<T0, T1, T2, T3, T4, T5, T6> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>) where
    T0: 'static,
    T1: 'static,
    T2: 'static,
    T3: 'static,
    T4: 'static,
    T5: 'static,
    T6: 'static, 
[src]

impl<T0, T1, T2, T3, T4, T5, T6, T7> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>) where
    T0: 'static,
    T1: 'static,
    T2: 'static,
    T3: 'static,
    T4: 'static,
    T5: 'static,
    T6: 'static,
    T7: 'static, 
[src]

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>, Option<T8>) where
    T0: 'static,
    T1: 'static,
    T2: 'static,
    T3: 'static,
    T4: 'static,
    T5: 'static,
    T6: 'static,
    T7: 'static,
    T8: 'static, 
[src]

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> TupleReducer for (Option<T0>, Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>, Option<T8>, Option<T9>) where
    T0: 'static,
    T1: 'static,
    T2: 'static,
    T3: 'static,
    T4: 'static,
    T5: 'static,
    T6: 'static,
    T7: 'static,
    T8: 'static,
    T9: 'static, 
[src]

Loading content...

Implementors

Loading content...