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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@resq-systems/math

v0.2.1

Published

Type-safe mathematical expression engine with sort-based dispatch, Pratt parser, and static validation

Readme

@resq-systems/math

Type-safe mathematical expression engine with sort-based dispatch, static validation, and a Pratt parser.

Install

bun add @resq-systems/math

Quick Start

import { N, S, add, mul, sum, v, evaluate, showValue } from "@resq-systems/math";

// (2 + 3) × 4 = 20
const expr = mul(add(N(2), N(3)), N(4));
console.log(showValue(evaluate(expr))); // "20"

// ∑_{i ∈ {1,2,3}} i × i = 14
const sigma = sum("i", S(1, 2, 3), mul(v("i"), v("i")));
console.log(showValue(evaluate(sigma))); // "14"

Parse from String

import { parse, evaluate, showValue } from "@resq-systems/math";

const expr = parse("(2 + 3) * 4");
console.log(showValue(evaluate(expr))); // "20"

// Unicode operators work too
const sets = parse("{1, 2, 3} ∪ {3, 4}");
console.log(showValue(evaluate(sets))); // "{1, 2, 3, 4}"

// Binders
const sigma = parse("sum(i in {1, 2, 3}, i * i)");
console.log(showValue(evaluate(sigma))); // "14"

Static Validation

import { checkExpr, add, N, B } from "@resq-systems/math";

// Check before evaluating — catches sort mismatches without side effects
const result = checkExpr(add(N(1), B(true)));
if (!result.ok) {
  console.log(result.errors); // [SortError: + is not defined on num × bool]
}

Pretty Printing

import { print, mul, add, N } from "@resq-systems/math";

const expr = mul(add(N(2), N(3)), N(4));
console.log(print(expr));                    // "(2 + 3) × 4"
console.log(print(expr, { ascii: true }));   // "(2 + 3) * 4"

Architecture

This library uses a compiler-style three-phase pipeline:

  1. Parse — Pratt parser converts strings to an AST (or build ASTs programmatically).
  2. Check — Static sort inference validates operator/domain compatibility.
  3. Evaluate — Tree-walking evaluator computes concrete values.

Why Not a Flat Dispatch Table?

A Record<Operator, (...args: number[]) => number> breaks down because:

  • Sorts vary — set ops return sets, relations return booleans.
  • Arity varies — unary (¬, √), binary (+, ∈), binders (∑, ∀) with scoped variables.
  • Overloading — + means addition on numbers, disjoint union on sets.

Instead, operators are dispatched via sort-keyed instance tables — the type-class pattern.

Operator Reference

Arithmetic (num × num → num)

+, -, × (*), ÷ (/), mod, pow (^)

Unary (num → num)

neg (-), sqrt, abs, floor, ceil, factorial (!)

Set Operations (set × set → set)

∪ (union), ∩ (intersect), ∖ (diff), △ (symdiff), + (disjoint union)

Set → Num

card (#) — cardinality

Relations (→ bool)

=, ≠ (!=), <, >, ≤ (<=), ≥ (>=), ∈ (in), ∉, ⊂ (subset), ⊆ (subseteq)

Logic (bool × bool → bool)

∧ (and, &&), ∨ (or, ||), ⊻ (xor), ⇒ (=>), ⇔ (<=>)

Binders

∑ (sum), ∏ (prod), ∀ (forall), ∃ (exists)

Control Flow

cond / if ... then ... else ...

Extensibility

Register custom operator instances at runtime:

import { registerBinary, num, asNum } from "@resq-systems/math";

// Add modular exponentiation: "modpow:num:num"
registerBinary("modpow:num:num", (a, b) => num(asNum(a) ** asNum(b) % 1000000007));

License

Apache-2.0