diff options
Diffstat (limited to 'receme/src/catana.rs')
-rw-r--r-- | receme/src/catana.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/receme/src/catana.rs b/receme/src/catana.rs new file mode 100644 index 0000000..e3d4728 --- /dev/null +++ b/receme/src/catana.rs @@ -0,0 +1,27 @@ +//! 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<T, F: Functor<T>, A: Algebra<T, F>> { + /// 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<T, F: Functor<T>, C: Coalgebra<T, F>> { + /// 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; +} |