import type { Recursive } from "@tsplus/stdlib/prelude/Recursive/definition" /** * Monadic fold over a datstructure `R` with an algebra `(r: Recursive) => M` * eventually returning an `M` * * @tsplus static Recursive/Aspects foldM * @tsplus pipeable Recursive foldM */ export function foldM( F: ForEach & Covariant, M: IdentityBoth & Monad, f: Recursive.FnM ) { const mapM = F.forEachF(M) const chain = DSL.flatMapF(M) const go = (term: Recursive): HKT.Kind => pipe( term.caseValue, mapM(go), chain(f) ) return (self: Recursive) => go(self) }