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

diagonalize

v0.1.7

Published

Searches through infinite branches

Downloads

16

Readme

Diagonalize

Allows you to create breadth-first recursive functions to search possibly infinite branches without getting stuck. For example:

// Searches for a 16-bit string following the 0101... pattern
function search(s = "") { 
  if (s.length === 8 && /^(01)*$/.test(s)) {
    return D.done(s);
  } else if (/(11)/.test(s) || /(00)/.test(s)) { // optimizes by pruning
    return D.fail("prune");
  } else {
    return D.wide([[search,[s+"0"]], [search,[s+"1"]]], (a) => D.done(a));
  };
};
console.log("found " + D.exec(() => search("")));

The program above searches all binary strings until it finds a 16-bit one with the 0101... pattern. It uses D.wide to suspend the execution of the function and recurse on multiple branches, descending diagonally until a value is returned with D.done, and then continuing on the callback. It optimizes the search by pruning bad branches (strings containing consecutive 1s or 0s) with a D.fail. You can also represent normal recursive calls (depth first) with D.deep, which immediately resumes the recursive branches:

// Computes 2^8 recursively
function pow(n = 0) { 
  if (n === 8) {
    return D.done(1);
  } else {
    return (
      D.deep([[pow, [n + 1]]], (a) => 
      D.deep([[pow, [n + 1]]], (b) => 
      D.done(a + b))));
  };
};
console.log("found " + D.exec(() => pow(0)));

By combining deep and wide, we're able to perform searches. For example, the program below enumerates all λ-terms until it finds λf. λx. (f (f (f (f x)))):

// Enumerates all lambda terms until it finds `λf. λx. (f (f (f (f x))))`
function show(term) {
  switch (term[0]) {
    case "Lam": return "λ"+show(term[1]);
    case "App": return "("+show(term[1])+" "+show(term[2])+")";
    case "Var": return term[1];
    case "Hol": return "_";
  };
};
function terms(term, depth = 0) {
  switch (term[0]) {
    // If term is a lambda, recurse on body
    case "Lam":
      return (
        D.deep([[terms, [term[1], depth + 1]]], (body) =>
        D.done(["Lam",body])));
    // If term is an application, recurse on func and argm
    case "App":
      return (
        D.deep([[terms, [term[1], depth]]], (func) =>
        D.deep([[terms, [term[2], depth]]], (argm) =>
        D.done(["App",func,argm]))));
    // If term is a variable, return it
    case "Var":
      return D.done(["Var",term[1]]);
    // If term is a hole, diagonalize through possible candidates  
    case "Hol":
      var branches = [];
      branches.push([terms, [["Lam",["Hol"]], depth]]);
      branches.push([terms, [["App",["Hol"],["Hol"]], depth]]);
      for (var i = 0; i < depth; ++i) {
        branches.push([terms, [["Var",i], depth]]);
      };
      return D.wide(branches, (hole) => D.done(hole));
    // The top term is used to receive all results of the search
    case "Top":
      return D.deep([[terms, [term[1], depth]]], (term) => {
        console.log("Generated:", show(term));
        if (show(term) === "λλ(1 (1 (1 (1 0))))") {
          return D.done(term);
        } else {
          return D.fail("Not it."); // continue search forever
        };
      });
  };
};
var done = D.exec(() => terms(["Top", ["Hol"]]));
console.log("Found:", show(done));

It works by creating lambda terms with holes, and using D.wide to make all possible instantiations of a hole. For example, the λf. λx. (f _) term widens to λf. λx. (f f), λf. λx. (f x), λf. λx. (f λz._), λf. λx. (f (_ _)). It keeps track of the binder count recursively, so that it only generates well scoped terms (no unbound variables).

The functions must be constructed in a monadic style. I tried using JavaScript generators, but ended up having problems since they are mutable, so I couldn't clone a suspended function state. I later found out a nice library that I could have used for this purpose, immutagen.