Trait naan::fold::Foldable

source ·
pub trait Foldable<F, A>where
    F: HKT1<T<A> = Self>,{
Show 15 methods // Required methods fn foldl<B, BAB>(self, f: BAB, b: B) -> B where BAB: F2<B, A, Ret = B>; fn foldr<B, ABB>(self, f: ABB, b: B) -> B where ABB: F2<A, B, Ret = B>; fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> B where BAB: F2<B, &'a A, Ret = B>, A: 'a; fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> B where ABB: F2<&'a A, B, Ret = B>, A: 'a; // Provided methods fn fold_map<AB, B>(self, f: AB) -> B where Self: Sized, AB: F1<A, Ret = B>, B: Monoid { ... } fn fold(self) -> A where Self: Sized, A: Monoid { ... } fn intercalate(self, sep: A) -> A where Self: Sized, A: Monoid + Clone { ... } fn contains<'a>(&'a self, a: &'a A) -> bool where &'a A: PartialEq, A: 'a { ... } fn not_contains<'a>(&'a self, a: &'a A) -> bool where &'a A: PartialEq, A: 'a { ... } fn any<'a, P>(&'a self, f: P) -> bool where P: F1<&'a A, Ret = bool>, A: 'a { ... } fn all<'a, P>(&'a self, f: P) -> bool where P: F1<&'a A, Ret = bool>, A: 'a { ... } fn length(&self) -> usize { ... } fn is_empty(&self) -> bool { ... } fn find_map<AB, B>(self, f: AB) -> Option<B> where Self: Sized, AB: F1<A, Ret = Option<B>> { ... } fn find<P>(self, f: P) -> Option<A> where Self: Sized, P: for<'a> F1<&'a A, Ret = bool> { ... }
}
Expand description

Foldable represents data structures which can be collapsed by starting with an initial value B, then a function that accumulates As into B.

Examples

fold instead of map followed by unwrap_or_else

use naan::prelude::*;

assert_eq!(Some(10).map(|a| i32::max(a, 20)).unwrap_or(20), 20);

assert_eq!(Some(10).foldl(i32::max, 20), 20);
assert_eq!(Some(10).foldl(i32::max, 1), 10);
assert_eq!(None.foldl(i32::max, 1), 1);

Collapsing a collection to a single value

use naan::prelude::*;

let ns = vec![4usize, 8, 10, 12];

let sum = ns.clone().foldl(|sum, cur| sum + cur, 0);
let product = ns.clone().foldl(|sum, cur| sum * cur, 1);

assert_eq!(sum, 34);
assert_eq!(product, 3840);

Picking a winning element from a collection

use naan::prelude::*;

fn is_prime(n: usize) -> bool {
  // <snip>
}

let ns = vec![4usize, 8, 10, 12];

let smallest = ns.clone()
                 .foldl(|largest: Option<usize>, cur| Some(largest.foldl(usize::min, cur)),
                        None);

let largest = ns.clone()
                .foldl(|largest: Option<usize>, cur| Some(largest.foldl(usize::max, cur)),
                       None);

let largest_prime =
  ns.clone().foldl(|largest_p: Option<usize>, cur| {
                     largest_p.foldl(|a: Option<usize>, p: usize| a.fmap(|a| usize::max(a, p)),
                                     Some(cur).filter(|n| is_prime(*n)))
                   },
                   None);

assert_eq!(largest, Some(12));
assert_eq!(smallest, Some(4));
assert_eq!(largest_prime, None);

Required Methods§

source

fn foldl<B, BAB>(self, f: BAB, b: B) -> Bwhere BAB: F2<B, A, Ret = B>,

Fold the data structure from left -> right

source

fn foldr<B, ABB>(self, f: ABB, b: B) -> Bwhere ABB: F2<A, B, Ret = B>,

Fold the data structure from right -> left

source

fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> Bwhere BAB: F2<B, &'a A, Ret = B>, A: 'a,

Fold the data structure from left -> right

source

fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> Bwhere ABB: F2<&'a A, B, Ret = B>, A: 'a,

Fold the data structure from right -> left

Provided Methods§

source

fn fold_map<AB, B>(self, f: AB) -> Bwhere Self: Sized, AB: F1<A, Ret = B>, B: Monoid,

Fold the data structure, accumulating the values into a Monoid.

use naan::prelude::*;

let nums = vec![0u8, 1, 2];

assert_eq!(nums.fold_map(|n: u8| format!("{n}")), format!("012"));
source

fn fold(self) -> Awhere Self: Sized, A: Monoid,

Accumulate the values in the data structure into a Monoid.

use naan::prelude::*;

let strings = vec![format!("a"), format!("b"), format!("c")];

assert_eq!(strings.fold(), format!("abc"));
source

fn intercalate(self, sep: A) -> Awhere Self: Sized, A: Monoid + Clone,

fold the elements into A when A is Monoid, inserting a separator between adjacent elements.

use naan::prelude::*;

let strings = vec![format!("a"), format!("b"), format!("c")];

assert_eq!(strings.intercalate(format!(", ")), format!("a, b, c"));
source

fn contains<'a>(&'a self, a: &'a A) -> boolwhere &'a A: PartialEq, A: 'a,

Test if the structure contains a value a

use naan::prelude::*;

let strings = vec![format!("a"), format!("b"), format!("c")];

assert_eq!(strings.contains(&format!("a")), true);
assert_eq!(strings.contains(&format!("d")), false);
source

fn not_contains<'a>(&'a self, a: &'a A) -> boolwhere &'a A: PartialEq, A: 'a,

Test if the structure does not contain a value a

use naan::prelude::*;

let strings = vec![format!("a"), format!("b"), format!("c")];

assert_eq!(strings.not_contains(&format!("a")), false);
assert_eq!(strings.not_contains(&format!("d")), true);
source

fn any<'a, P>(&'a self, f: P) -> boolwhere P: F1<&'a A, Ret = bool>, A: 'a,

Test if any element in the structure satisfies a predicate f

use naan::prelude::*;

let strings = vec![format!("ab"), format!("cde")];

assert_eq!(strings.any(|s: &String| s.len() > 2), true);
source

fn all<'a, P>(&'a self, f: P) -> boolwhere P: F1<&'a A, Ret = bool>, A: 'a,

Test if every element in the structure satisfies a predicate f

use naan::prelude::*;

let strings = vec![format!("ab"), format!("cde")];

assert_eq!(strings.all(|s: &String| s.len() < 4), true);
source

fn length(&self) -> usize

Get the number of elements contained within the structure

use naan::prelude::*;

assert_eq!(Vec::<()>::new().length(), 0);
assert_eq!(vec![(), (), ()].length(), 3);
source

fn is_empty(&self) -> bool

Test if the structure is empty

use naan::prelude::*;

assert_eq!(Vec::<()>::new().is_empty(), true);
assert_eq!(vec![()].is_empty(), false);
source

fn find_map<AB, B>(self, f: AB) -> Option<B>where Self: Sized, AB: F1<A, Ret = Option<B>>,

Fold values until a match is found

source

fn find<P>(self, f: P) -> Option<A>where Self: Sized, P: for<'a> F1<&'a A, Ret = bool>,

Fold values until a match is found

Implementations on Foreign Types§

source§

impl<A, const N: usize> Foldable<ArrayVec<N>, A> for ArrayVec<[Option<A>; N]>

source§

fn foldl<B, BAB>(self, f: BAB, b: B) -> Bwhere BAB: F2<B, A, Ret = B>,

source§

fn foldr<B, ABB>(self, f: ABB, b: B) -> Bwhere ABB: F2<A, B, Ret = B>,

source§

fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> Bwhere BAB: F2<B, &'a A, Ret = B>, A: 'a,

source§

fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> Bwhere ABB: F2<&'a A, B, Ret = B>, A: 'a,

source§

impl<A, E> Foldable<ResultOk<E>, A> for Result<A, E>

source§

fn foldl<B, BAB>(self, f: BAB, b: B) -> Bwhere BAB: F2<B, A, Ret = B>,

source§

fn foldr<B, ABB>(self, f: ABB, b: B) -> Bwhere ABB: F2<A, B, Ret = B>,

source§

fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> Bwhere BAB: F2<B, &'a A, Ret = B>, A: 'a,

source§

fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> Bwhere ABB: F2<&'a A, B, Ret = B>, A: 'a,

source§

impl<A, K> Foldable<BTreeMapValues<K>, A> for BTreeMap<K, A>where K: Ord,

source§

fn foldl<B, BAB>(self, f: BAB, b: B) -> Bwhere BAB: F2<B, A, Ret = B>,

source§

fn foldr<B, ABB>(self, f: ABB, b: B) -> Bwhere ABB: F2<A, B, Ret = B>,

source§

fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> Bwhere BAB: F2<B, &'a A, Ret = B>, A: 'a,

source§

fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> Bwhere ABB: F2<&'a A, B, Ret = B>, A: 'a,

source§

impl<A> Foldable<Option, A> for Option<A>

source§

fn foldl<B, BAB>(self, f: BAB, b: B) -> Bwhere BAB: F2<B, A, Ret = B>,

source§

fn foldr<B, ABB>(self, f: ABB, b: B) -> Bwhere ABB: F2<A, B, Ret = B>,

source§

fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> Bwhere BAB: F2<B, &'a A, Ret = B>, A: 'a,

source§

fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> Bwhere ABB: F2<&'a A, B, Ret = B>, A: 'a,

source§

impl<A, K> Foldable<HashMapValues<K>, A> for HashMap<K, A>where K: Eq + Hash,

source§

fn foldl<B, BAB>(self, f: BAB, b: B) -> Bwhere BAB: F2<B, A, Ret = B>,

source§

fn foldr<B, ABB>(self, f: ABB, b: B) -> Bwhere ABB: F2<A, B, Ret = B>,

source§

fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> Bwhere BAB: F2<B, &'a A, Ret = B>, A: 'a,

source§

fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> Bwhere ABB: F2<&'a A, B, Ret = B>, A: 'a,

source§

impl<A> Foldable<Vec, A> for Vec<A>

source§

fn foldl<B, BAB>(self, f: BAB, b: B) -> Bwhere BAB: F2<B, A, Ret = B>,

source§

fn foldr<B, ABB>(self, f: ABB, b: B) -> Bwhere ABB: F2<A, B, Ret = B>,

source§

fn foldl_ref<'a, B, BAB>(&'a self, f: BAB, b: B) -> Bwhere BAB: F2<B, &'a A, Ret = B>, A: 'a,

source§

fn foldr_ref<'a, B, ABB>(&'a self, f: ABB, b: B) -> Bwhere ABB: F2<&'a A, B, Ret = B>, A: 'a,

Implementors§