Skip to main content

Alternative

Trait Alternative 

Source
pub trait Alternative: Applicative + Plus { }
Expand description

A type class combining Applicative and Plus.

Alternative has no members of its own; it specifies that the type constructor has both Applicative and Plus instances.

§Laws

Alternative instances must satisfy the following laws:

  • Distributivity: apply(alt(f, g), x) = alt(apply(f, x), apply(g, x)).
  • Annihilation: apply(empty, f) = empty.

§Examples

Alternative laws for Vec:

use fp_library::{
	brands::*,
	classes::*,
	functions::*,
};

// Annihilation: apply(empty, f) = empty
let f: Vec<i32> = vec![1, 2, 3];
let empty_fns: Vec<std::rc::Rc<dyn Fn(i32) -> i32>> = plus_empty::<VecBrand, _>();
let result = apply(empty_fns, f);
assert_eq!(result, plus_empty::<VecBrand, i32>());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<Brand> Alternative for Brand
where Brand: Applicative + Plus,

Blanket implementation of Alternative.

§Type Parameters
  • Brand: The brand type.