summaryrefslogtreecommitdiff
path: root/receme/src/catana.rs
blob: e3d4728cc8ffdeed08f2bb6d9e2570a457c24fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}