Skip to content

Instantly share code, notes, and snippets.

@divs1210
divs1210 / ccAndEval.js
Last active September 27, 2024 08:31
Simple Evaluator vs Compile to Closure Evaluator benchmark
// calculates factorials of numbers from 1 to 20
const code =
["do",
["var", "N", 20],
["var", "i", 1],
["while", ["<=", "i", "N"],
["do",
["var", "n", "i"],
["var", "f", 1],
["while", [">", "n", 0],
@divs1210
divs1210 / primes.js
Created July 18, 2024 06:37
Memoized first N primes in JS
const any = (arr, f) => {
for (const x of arr) {
if (f(x))
return true;
}
return false;
}
const KNOWN_PRIMES = [2];
const primes = (n) => {
@divs1210
divs1210 / transducerExpansion.js
Last active March 4, 2024 13:03
Transducer expansion
function mapping(f) {
return function(rf) {
return function(acc, x) {
return rf(acc, f(x))
}
}
}
function filtering(f) {
return function(rf) {
@divs1210
divs1210 / AVLSort.js
Created February 28, 2024 18:03
Sorting using self-balancing AVL tree
function getHeight(tree) {
return tree === null? 0: tree.height
}
function newHeight(leftSubtree, rightSubtree) {
return Math.max(getHeight(leftSubtree), getHeight(rightSubtree)) + 1
}
function insert(tree, e) {
if(tree === null)
@divs1210
divs1210 / sequence.clj
Last active March 7, 2024 10:06
clojure.core/sequence implemented in plain Clojure
(ns sequence
(:refer-clojure :exclude [sequence sort])
(:require [clojure.core :as core]))
;; Helper
;; ======
(defn accumulate
([])
([acc] acc)
([acc x] (conj (vec acc) x)))
@divs1210
divs1210 / react-starter.html
Created September 25, 2023 19:39
react-browser-starter
<!DOCTYPE html>
<html lang="en">
<title>Test React</title>
<head>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
@divs1210
divs1210 / csp.js
Created September 13, 2023 09:09
Go-style CSP in JavaScript
const CLOSED = Symbol('chan-closed');
const StackGC = async () => null;
class Chan {
constructor(size) {
this.q = [];
this.size = size || 1;
this.isClosed = false;
}
@divs1210
divs1210 / StackGC.js
Last active January 10, 2024 07:19
Async JS is Stackless
// StackGC
// =======
const StackGC = async () => null;
// Tests
// =====
// util
function assert(assertion, msg) {
console.assert(assertion, `%s`, msg);
@divs1210
divs1210 / stackless-eval-playground.html
Last active September 13, 2023 09:42
Stackless Eval Playground
<html>
<head>
<script type="text/javascript">
// Trampoline
// ==========
class Thunk {
constructor(f) {
this.f = f;
}
}
@divs1210
divs1210 / stackless-eval.md
Last active April 22, 2024 01:58
Writing a Stackless Evaluator

Writing a Stackless Evaluator

Divyansh Prakash, September 2023

tiny-stackless-eval

Preface

NOTE: Please read the previous post to understand the context of this post.