Skip to content

Instantly share code, notes, and snippets.

View jviide's full-sized avatar
📴

jviide

📴
View GitHub Profile
@jviide
jviide / example.ts
Last active March 19, 2021 12:06
A small-ish TypeScript LRU cache implementation for string inputs
import { memoized } from "./lru";
const double = memoized((a) => {
return a + a;
});
console.log(double("jee"));
@jviide
jviide / README.md
Last active August 26, 2020 14:58
A fun lazy / incremental calculation helper

A short-ish example of incremental & lazy alternative to Array#reduce when you have a bunch of rapidly changing values.

The basic idea is to use a normal array and push values to it, but also think of the array as a representation of a binary tree (using the scheme outlined in Wikipedia). Each tree node keeps track of the reduced value of the subtree under it, and whenever something changes (i.e. a value is pushed to the array, a value is removed etc.) the changed node and its ancestors are marked to be "dirty", i.e. needing recalculation. All the dirty nodes get refreshed lazily when the reduced value for the tree is needed.

Each single tree modification marks at most 2 * log N nodes dirty (where N is the number of nodes in the tree). Therefore calculating the reduced value takes at O(min(m log N, N)) time (where m is the number of single modifications done to the tree since the last time the reduced value was calculated), assuming the given red

@jviide
jviide / README.md
Last active November 7, 2020 22:39

A Small HTM VM FTW OMG BBQ

This is a half-baked sketch of compiling HTM expressions into a list of instructions that can be interpreted in runtime with a compact function. The instructions hopefully compress well and the evaluation function should be not-slow. Maybe.

  • evaluate.js contains the evaluation function (evaluate(...)) as well as an example of a compiled HTM call.

  • babel.js contains a Babel plugin that transforms HTM calls into the stack language. The build.mjs file it imports is HTM's src/build.mjs.

This tweet demonstrates that a minified evaluate() fits in one tweet: https://twitter.com/jviide/status/1257755526722662405 🙂

# __pragma__ ('skip')
from htm import htm
# __pragma__ ('noskip')
# __pragma__ ('ecom')
'''?
__pragma__('js', """
var module = {{}};
{}
var htmjs = module.exports;
@jviide
jviide / README.md
Last active March 21, 2020 19:06
Proof-of-concept: Generating HTML with htm.py

Proof-of-concept: Generating HTML with htm.py

A demo of generating HTML with just htm.py and the Python 3 standard library. This idea could, maybe, be combined with this gist.

h.py is the main "library" that implements html for generating the DOM and render for rendering it.

example.py uses h.py and illustrates some general patterns, such as:

  • functional components (see functional_component)
  • generator components (see generator_component)
@jviide
jviide / README.md
Last active October 19, 2019 13:17
Proof-of-concept: Universal htm.py with the Python-to-JS compiler Transcrypt

Proof-of-concept: Universal htm.py with the Python-to-JS compiler Transcrypt

This is a Rube Goldberg machine that aims to demonstrate how htm.py could be used with the Python-to-JS compiler Transcrypt. This idea could, maybe, be combined with this gist.

It contains the following moving parts:

  • myhtm.py is a wrapper for htm.py that transparently switches to developit/htm when it's compiled with Transcrypt.

  • compile.py precompiles Python 3 code from STDIN to one that doesn't depend on tagged's stack inspection shenanigans (which htm.py depends on). This should make the code more compatible with Transcrypt.

@jviide
jviide / README.md
Last active October 4, 2019 21:10

A proof-of-concept Python code rewriter, transforming JavaScript-like tagged template strings (tag"foo{1+2}bar") to plain function calls (tag(["foo", "bar"], [1+2])).

Note that the code requires tagged version 0.0.2 or higher.

You can use ./encoder.py file to transform STDIN input to STDOUT:

$ python3 encoder.py < example_input.py
@jviide
jviide / example.py
Last active August 9, 2019 17:38
htm.py ❤️ hyperpython
from htm import htm
from hyperpython import h
from inspect import signature, Parameter
@htm
def html(tag, props, children):
if callable(tag):
return relaxed_call(tag, children=children, **props)
return h(tag, props, children)
@jviide
jviide / counting-sort-magick.js
Last active March 3, 2020 07:40
Sorting by __depth with counting sort (O(n) time vs. the usual O(n log n), and sorting is stable)
function sorted(array) {
const maxDepth = array.reduce((acc, cur) => acc > cur.__depth ? acc : cur.__depth, 0);
const offsets = new Array(maxDepth + 1).fill(0);
array.forEach(item => {
offsets[item.__depth]++;
});
let offset = 0;
offsets.forEach((count, index) => {
offsets[index] = offset;
@jviide
jviide / heap-magick.js
Last active April 12, 2019 17:31
Using a heap to sort by __depth
function push(array, item) {
let index = array.length;
while (index > 0) {
const parentIndex = ((index - 1) / 2) | 0;
if (array[parentIndex].__depth <= item.__depth) {
break;
}
array[index] = array[parentIndex];
index = parentIndex;
}