MVFM

dagql/selectWhere

Query the AST — find node IDs matching a predicate (byKind, isLeaf, etc.).

// 1. make an app
const app = mvfm(prelude, console_);

// 2. make a program: (x + 10) * (x - 3)
const prog = app({ x: "number" }, ($) =>
  $.mul($.add($.input.x, 10), $.sub($.input.x, 3))
);

const injected = injectInput(prog, { x: 5 });

// 3. dagql: query the AST with different predicates
const adds = selectWhere(injected.__nexpr, byKind("num/add"));
const leaves = selectWhere(injected.__nexpr, isLeaf());
const binary = selectWhere(injected.__nexpr, hasChildCount(2));
const allNum = selectWhere(injected.__nexpr, byKindGlob("num/"));

console.log("add nodes:", adds.size);
console.log("leaf nodes:", leaves.size);
console.log("binary ops:", binary.size);
console.log("all num nodes:", allNum.size);

// 4. fold the original
await fold(defaults(app), injected);
Ctrl+Enter