npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

church-cat

v1.0.0

Published

Run catamorphisms and other recursion schemes over church-encoded ADTs

Downloads

9

Readme

Church Cat: Recursion schemes over church-encoded data

Church cat solves a problem that often comes up in language tooling like parsers and compilers. Given a tree of data, e.g. an abstract syntax tree or context-free grammar, we want to compute several values at each node. These values might depend on each other and should not be recomputed.

There are various ways to approach the problem: adding extra data to each node that is filled in by passes over the tree (downsides: the dependency between different pieces of data is implicit). Attribute grammars. Or recursion schemes like catamorphisms. This library is based on recursion schemes, although it plays much looser with them than what you'd find in Haskell.

const { I, K, cata, constructors } = require("church-cat");
// declare a schema using I and K. 
// I means a recursive copy of the top-level structure. 
// K means any other type.
const treeSchema = {
    branch: [I, I],
    tip: [K]
};

let { branch: Branch, tip: Tip } = constructors(treeSchema);
let exampleTree = Branch(
    Branch(tip(1), tip(2)), 
    tip(3));

// the return value of the constructors will be a church-encoded ADT, with a link to the associated schema.
// you can pattern match on the ADT simply by calling it with an object literal.
exampleTree({
    branch: (l,r) => console.log("example tree is a branch"), 
    tip: (n) => console.log("example tree is a tip")
});

// you declare a catamorphism by passing in a top-level ADT, and an object literal with reduction functions.
let heightOf = cata(exampleTree, {
    branch: (l,r) => 1 + Math.max(l, r),
    tip: (n) => 1
});

// after declaring a catamorphism, you can call it on any child of the ADT. 
// The catamorphism will only run once, memoizing its results.
heightOf(exampleTree); // 3
exampleTree({branch: (l,r) => [heightOf(l), heightOf(r)]}); // [2, 1]

// catamorphisms can also be declared with a seed argument in the third
// position. If there is a seed argument, then it will be passed down through
// the tree of catamorphism values.

// Important: Every reduction function must call all its children at least once.

// this rather useless catamorphism will find an array of parent heights, e.g. [3,2,1] for a tip at depth 3.
let parentHeights = cata(exampleTree, {
    branch: function(l,r) { 
        return parents => {
            // `this` inside a catamorphism is the corresponding ADT. Use `this` to call other catamorphisms.
            let myHeight = heightOf(this);
            // both children, l and r, must be called
            l([...parents, myHeight]); 
            r([...parents, myHeight]); 
            return parents
        }
    },
    tip: (n) => parents => parents
}, []);