//! This file defines behaviours of catamorphisms and anamorphisms. //! //! A catamorphism collapses a recursive structure into a flat value, //! whereas an anamorphism expands a value into a recursive structure. use crate::{algebra::Algebra, coalgebra::Coalgebra, functor::Functor}; /// A type that implements Cata is capable of being collapsed into a single value. /// /// The catamorphism consumes `self`. pub trait Cata, A: Algebra> { /// A catamorphism takes a recursive structure and an algebra for /// the recursive structure, and returns a single, flat, collapsed /// value. fn cata(self, alg: A) -> T; } /// A type that implements Ana is capable of being expanded from a /// flat value. /// /// The anamorphism consumes `self`. pub trait Ana, C: Coalgebra> { /// An anamorphism takes a single, flat, collapsed value and a /// co-algebra for a recursive structure, and returns that /// recursive structure. fn ana(value: T, coalg: C) -> Self; }