Skip to main content

Comonad

Trait Comonad 

Source
pub trait Comonad: Extend + Extract { }
Expand description

A type class for comonads, combining Extend and Extract.

class (Extend w, Extract w) => Comonad w

Comonad is the dual of Monad. Where a Monad lets you sequence effectful computations by injecting values (pure) and chaining with bind, a Comonad lets you observe values (extract) and extend local computations to global ones (extend).

§Laws

A lawful Comonad must satisfy three laws:

  1. Left identity: extracting after extending recovers the function’s result.

    extract(extend(f, wa)) == f(wa)
  2. Right identity: extending with extract is a no-op.

    extend(extract, wa) == wa
  3. Map-extract: extracting after mapping is the same as applying the function to the extracted value.

    extract(map(f, wa)) == f(extract(wa))

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> Comonad for Brand
where Brand: Extend + Extract,

Blanket implementation of Comonad.

§Type Parameters
  • Brand: The brand type.