diff --git a/prototype/Doubt/Cofree.swift b/prototype/Doubt/Cofree.swift index 074870908..971a8b4da 100644 --- a/prototype/Doubt/Cofree.swift +++ b/prototype/Doubt/Cofree.swift @@ -120,6 +120,15 @@ extension CofreeType { return (term.extract, term.unwrap) } + + /// Catamorphism over `TermType`s. + /// + /// Folds the tree encoded by the receiver into a single value by recurring top-down through the tree, applying `transform` to leaves, then to branches, and so forth. + public func cata(transform: Syntax -> Result) -> Result { + return self |> (Self.unwrap >>> { $0.map { $0.cata(transform) } } >>> transform) + } + + /// Constructs a cofree by coiteration. /// /// This is an _anamorphism_ (from the Greek “ana,” “upwards”; compare “anabolism”), a generalization of unfolds over regular trees (and datatypes isomorphic to them). The initial seed is used as the annotation of the returned value. The continuation of the structure is unpacked by applying `annotate` to the seed and mapping the resulting syntax’s values recursively. In this manner, the structure is unfolded bottom-up, starting with `seed` and ending at the leaves.