Skip to main content

FoldableA

Trait FoldableA 

Source
pub trait FoldableA<'r, A: 'r>: HKST<'r, A> {
    // Required methods
    fn fold<F>(&'r self, z: A, f: F) -> A
       where F: FnMut(A, &A) -> A;
    fn find<F>(&'r self, f: F) -> Option<&A>
       where F: Fn(&A) -> bool;
    fn all<F>(&'r self, f: F) -> bool
       where F: Fn(&A) -> bool;
    fn any<F>(&'r self, f: F) -> bool
       where F: Fn(&A) -> bool;
    fn filter<F>(&'r self, f: F) -> Self::M
       where F: Fn(&A) -> bool;
    fn is_empty(&'r self) -> bool;

    // Provided methods
    fn concat(&'r self) -> A
       where A: Monoid { ... }
    fn non_empty(&'r self) -> bool { ... }
}
Expand description

FoladableA is for endo type functions

Required Methods§

Source

fn fold<F>(&'r self, z: A, f: F) -> A
where F: FnMut(A, &A) -> A,

Reduces the values of the Foldable into a single value

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
let sum = v.fold(0, |b, a| a + b);
assert_eq!(10, sum);
Source

fn find<F>(&'r self, f: F) -> Option<&A>
where F: Fn(&A) -> bool,

Find a value in the foldable, returns an Option<&_>

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
let s = v.find(|&a| a == 2);
let n = v.find(|&a| a == 5);
assert_eq!(Some(&2), s);
assert_eq!(None, n);
Source

fn all<F>(&'r self, f: F) -> bool
where F: Fn(&A) -> bool,

Check if all values in the foldable returns true for function f

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
assert_eq!(true, v.all(|&a| a < 5));
assert_eq!(false, v.all(|&a| a < 4));
Source

fn any<F>(&'r self, f: F) -> bool
where F: Fn(&A) -> bool,

Check if any valu ein the foldable returns true for function f

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
assert_eq!(true, v.any(|&a| a == 4));
assert_eq!(false, v.any(|&a| a == 5));
Source

fn filter<F>(&'r self, f: F) -> Self::M
where F: Fn(&A) -> bool,

Filters the foldable for values that meet the predicate

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
assert_eq!(vec![&1,&2], v.filter(|&a| a < 3));
Source

fn is_empty(&'r self) -> bool

Checks if the foldable is empty

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
let v2: Vec<i32> = vec![];
assert_eq!(false, v.is_empty());
assert_eq!(true, v2.is_empty());

Provided Methods§

Source

fn concat(&'r self) -> A
where A: Monoid,

Using a Monoid reduce the values in the Foldable to a single value

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
let sum = v.concat();
assert_eq!(10, sum);
Source

fn non_empty(&'r self) -> bool

Checks if the foldable is non empty

§Examples
use funlib::Foldable::*;
let v = vec![1,2,3,4];
let v2: Vec<i32> = vec![];
assert_eq!(true, v.non_empty());
assert_eq!(false, v2.non_empty());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'r, A: 'r> FoldableA<'r, A> for Vec<A>

Source§

fn fold<F>(&'r self, z: A, f: F) -> A
where F: FnMut(A, &A) -> A,

Source§

fn all<F>(&'r self, f: F) -> bool
where F: Fn(&A) -> bool,

Source§

fn any<F>(&'r self, f: F) -> bool
where F: Fn(&A) -> bool,

Source§

fn filter<F>(&'r self, f: F) -> Self::M
where F: Fn(&A) -> bool,

Source§

fn find<F>(&'r self, f: F) -> Option<&A>
where F: Fn(&A) -> bool,

Source§

fn is_empty(&'r self) -> bool

Implementors§