Skip to main content

Cofree

Struct Cofree 

Source
pub struct Cofree<F, A>
where F: HKT,
{ pub head: A, pub tail: Box<<F as HKT>::Of<Cofree<F, A>>>, }
Expand description

Cofree Comonad — the dual of the Free Monad.

Cofree<F, A> pairs a value A at each node with a branching structure determined by F. The choice of F determines the shape:

  • OptionF → a non-empty list (finite stream)
  • VecF → a rose tree
  • IdentityF → an infinite stream

Each node carries a value (head) and subtrees (tail).

Fields§

§head: A

The value at this node.

§tail: Box<<F as HKT>::Of<Cofree<F, A>>>

The subtrees, with branching structure determined by F.

Implementations§

Source§

impl<F, A> Cofree<F, A>
where F: HKT,

Source

pub fn new(head: A, tail: <F as HKT>::Of<Cofree<F, A>>) -> Cofree<F, A>

Create a Cofree node with the given head and tail.

Source

pub fn extract(&self) -> A
where A: Clone,

Extract the head value.

Source§

impl<F, A> Cofree<F, A>
where F: HKT + Functor,

Source

pub fn fmap<B>(self, f: impl Fn(A) -> B) -> Cofree<F, B>

Map a function over all head values in the tree.

Source

pub fn extend<B>(self, f: impl Fn(&Cofree<F, A>) -> B) -> Cofree<F, B>
where A: Clone,

Apply a context-aware function to every position in the tree.

At each node, f receives the entire sub-cofree rooted at that node and produces the new head value.

Source

pub fn unfold<Seed>( seed: Seed, f: impl Fn(&Seed) -> (A, <F as HKT>::Of<Seed>), ) -> Cofree<F, A>

Build a Cofree from a seed value and an unfolding function.

The function takes a seed and returns (head, F<Seed>) — the value at this node and seeds for the subtrees.

Auto Trait Implementations§

§

impl<F, A> Freeze for Cofree<F, A>
where A: Freeze,

§

impl<F, A> RefUnwindSafe for Cofree<F, A>
where A: RefUnwindSafe, <F as HKT>::Of<Cofree<F, A>>: RefUnwindSafe,

§

impl<F, A> Send for Cofree<F, A>
where A: Send, <F as HKT>::Of<Cofree<F, A>>: Send,

§

impl<F, A> Sync for Cofree<F, A>
where A: Sync, <F as HKT>::Of<Cofree<F, A>>: Sync,

§

impl<F, A> Unpin for Cofree<F, A>
where A: Unpin,

§

impl<F, A> UnsafeUnpin for Cofree<F, A>
where A: UnsafeUnpin,

§

impl<F, A> UnwindSafe for Cofree<F, A>
where A: UnwindSafe, <F as HKT>::Of<Cofree<F, A>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.