Skip to main content

Plus

Trait Plus 

Source
pub trait Plus: Alt {
    // Required method
    fn empty<'a, A: 'a>() -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>;
}
Expand description

A type class extending Alt with an identity element.

Plus is similar to Monoid, except that it applies to types of kind * -> * (like Option or Vec) rather than concrete types.

§Laws

Plus instances must satisfy the following laws:

  • Left identity: alt(empty, x) = x.
  • Right identity: alt(x, empty) = x.
  • Annihilation: map(f, empty) = empty.

§Examples

Plus laws for Option:

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

// Left identity: alt(empty, x) = x
let x = Some(5);
assert_eq!(alt::<OptionBrand, _>(plus_empty::<OptionBrand, i32>(), x), x,);

// Right identity: alt(x, empty) = x
assert_eq!(alt::<OptionBrand, _>(x, plus_empty::<OptionBrand, i32>()), x,);

// Annihilation: map(f, empty) = empty
let f = |i: i32| i * 2;
assert_eq!(
	map::<OptionBrand, _, _>(f, plus_empty::<OptionBrand, i32>()),
	plus_empty::<OptionBrand, i32>(),
);

Required Methods§

Source

fn empty<'a, A: 'a>() -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>

Returns the identity element for alt.

This is the empty/failure value for the type constructor. For Option, this is None. For Vec, this is vec![].

§Type Signature

forall A. () -> Self A

§Type Parameters
  • 'a: The lifetime of the value.
  • A: The type of the value inside the context.
§Returns

The identity element.

§Examples
use fp_library::{
	brands::*,
	functions::*,
};

let x: Option<i32> = plus_empty::<OptionBrand, i32>();
assert_eq!(x, None);

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§