Skip to content

Instantly share code, notes, and snippets.

View dherman's full-sized avatar

Dave Herman dherman

View GitHub Profile
@dherman
dherman / ab.js
Created July 15, 2022 16:51
failed experiment to monkey-patch ArrayBuffer.prototype
"use strict";
{
let abp = ArrayBuffer.prototype;
const VIEW = Symbol('view');
function getView(receiver) {
return receiver[VIEW] ||
(receiver[VIEW] = new Uint8Array(receiver));
}
flowchart TB
    JsValue
    JsValue-->JsObject
    subgraph primitives [Primitive Types]
        JsBoolean
        JsNumber
        JsString
        JsNull
 JsUndefined
@dherman
dherman / template-literals.md
Last active July 4, 2021 10:41
What can you do with ES6 string template literals?

DOM selectors

var elements = query`.${className}`;

Localization

var message = l10n`Hello ${name}; you are visitor number ${visitor}:n!

Motivation

  • expression-oriented programming one of the great advances of FP
  • expressions plug together like legos, making more malleable programming experience in-the-small

Examples

Write in an expression-oriented style, scoping variables as locally as possible:

@dherman
dherman / monocle-mustache-semi.js
Created December 1, 2011 08:24
Monocle-mustache using semicolons (intuition: blocks and statements, not object literals)
// inspired by https://github.com/raganwald/homoiconic/blob/master/2011/11/sans-titre.md#readme
// and by https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/611c04100ac17142
// traditional method chaining with combinators:
console.log(range(1, 3)
.concat(range(4, 6))
.map(function(x) { return x * x })
.filter(function(x) { return x % 2 === 0 })
.reverse());
@dherman
dherman / combos.js
Created February 11, 2020 18:29
quick n dirty generator of 2^6 combinations
const ATTRIBUTES = ['A', 'B', 'C', 'D', 'E', 'F'];
// type attribute = 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
// type value = "x" | ""
// type combo = [value]
function combos() {
return combosFrom(ATTRIBUTES, 0);
}
@dherman
dherman / geomean.js
Last active January 9, 2020 23:28
calculating the geometric mean in normal JS and asm.js
// Calculating the geometric mean in log space avoids overflow when
// working with anything more than very small data sets.
//
// The formula can be calculated with simple algebraic identities:
//
// n√(Prod^n x_i)
// = { e^ln(x) = x }
// n√(Prod^n e^ln(x_i))
// = { e^a x e^b = e^(a+b) }
// n√(e^(Sum^n ln(x_i)))
@dherman
dherman / 1-recursive.js
Created February 7, 2012 20:10
turning recursion into iteration in JS
// Version 1. Simple recursive function. Blows the stack for large nodes.
function replace(node, from, to) {
switch (node.type) {
case IF:
return {
type: IF,
test: replace(node.test, from, to),
then: replace(node.then, from, to),
else: replace(node.else, from, to)
};
@dherman
dherman / 1-import-meta.js
Last active September 4, 2019 18:13
using new.target-style syntax for providing metadata and relative dynamic import to modules
// import default export
import.default("Spinner").then(Spinner => {
widget.appendChild(new Spinner(...))
});
// import namespace object
import.namespace("fs").then(fs => {
fs.readFile(...)
});
@dherman
dherman / javascript.js
Created November 28, 2012 00:27
javascript
(({}+[])[!![]+!![]+!![]]+[]+(![]+[])[+!![]]+(+(!![]+!![]+!![]+[]+(+!![])))[(!![]+[])[+![]]+[]+({}+[])[+!![]]+[][(![]+[])[!![]+!![]+!![]]+[]+(![]+[])[!![]+!![]]+([][![]]+[])[!![]+!![]+!![]+!![]+!![]]+({}+[])[!![]+!![]+!![]+!![]+!![]]+(!![]+[])[!![]+!![]+!![]]][({}+[])[!![]+!![]+!![]+!![]+!![]]+[]+(![]+[])[+!![]]+(![]+[])[!![]+!![]]+(![]+[])[!![]+!![]]](+[![]]+[]+(!![]+[])[({}+[])[!![]+!![]+!![]+!![]+!![]]+[]+({}+[])[+!![]]+([][![]]+[])[+!![]]+(![]+[])[!![]+!![]+!![]]+(!![]+[])[+![]]+(!![]+[])[+!![]]+([][![]]+[])[+![]]+({}+[])[!![]+!![]+!![]+!![]+!![]]+(!![]+[])[+![]]+({}+[])[+!![]]+(!![]+[])[+!![]]])[(![]+[])[+![]]+[]+([][![]]+[])[!![]+!![]+!![]+!![]+!![]]+(![]+[])[!![]+!![]]+(!![]+[])[+![]]+(!![]+[])[!![]+!![]+!![]]+(!![]+[])[+!![]]]([][({}+[])[!![]+!![]+!![]+!![]+!![]]+[]+({}+[])[+!![]]+([][![]]+[])[+!![]]+(![]+[])[!![]+!![]+!![]]+(!![]+[])[+![]]+(!![]+[])[+!![]]+([][![]]+[])[+![]]+({}+[])[!![]+!![]+!![]+!![]+!![]]+(!![]+[])[+![]]+({}+[])[+!![]]+(!![]+[])[+!![]]][({}+[])[!![]+!![]+!![]+!![]+!![]]+[]+({}+[])[