Skip to content

Instantly share code, notes, and snippets.

function id(x) {
return x;
}
// Memoize a function -- creates a new function that will cache the results of
// the first return by serializing inputs as a key.
//
// * `fn`: the function to be memoized.
// * `hash`: a function to create the cache key.
// * `out`: a function to process memoized data on the way out. Useful if you need
@gordonbrander
gordonbrander / n-vector.js
Last active October 29, 2023 13:00
n-vector: generic, efficient JavaScript vector math using ordinary arrays or indexed objects
/*
n-vector.js
Copyright (c) 2014 Gordon Brander
Released under the MIT license
http://opensource.org/licenses/MIT
Generic, efficient JavaScript vector math using ordinary arrays
or indexed objects.
@gordonbrander
gordonbrander / gordonbrander-highlighter.css
Last active August 29, 2015 13:56
Highlighter for Markdown
/*
This document has been created with Marked.app <http://markedapp.com>, Copyright 2011 Brett Terpstra
Please leave this notice in place, along with any additional credits below.
---------------------------------------------------------------
Title: Highlighter
Author: Gordon Brander http://gordonbrander.com
Description: Minimal theme with careful typographic scale and highlighed bold.
*/
/* http://retinart.net/typography/typographicscale/ */
html {
@gordonbrander
gordonbrander / min-event-behavior.js
Last active August 21, 2018 11:57
Minimal FRP events and behaviors
// Minimal FRP Behaviors and Events.
// An event function is any function of shape `function (next) { ... }` where
// `next(value)` is a callback to be called by event function. Transformations
// of event are accomplished by wrapping event with another event function,
// and consuming original event within (CPS).
// A behavior is any function of shape `function (time) { ... }`, where
// `time` is current time. Behaviors may capture state, return value from time,
// or be constant. Behaviors must always return a value, but value may
@gordonbrander
gordonbrander / smd.js
Last active September 3, 2017 05:16 — forked from anonymous/smd.js
(function(exports) {
var modules = {}
var factories = {}
// Require a module by id.
function require(id) {
if (!(id in factories)) throw Error(id + ' module is not defined')
if (!(id in modules)) {
modules[id] = {}
factories[id](require, modules[id])
@gordonbrander
gordonbrander / dispatches.js
Created January 18, 2014 00:49
Distpatches -- MVP signals.
// Reduce any arraylike object.
function reduceIndexed(indexed, next, initial) {
var accumulated = initial;
for (var i = 0; i < indexed.length; i += 1)
accumulated = next(accumulated, indexed[i]);
return accumulated;
}
@gordonbrander
gordonbrander / enumerate.js
Created December 17, 2013 17:43
Enumerate object's own keys
// Enumerate over an object's own keys/values, accumulating a value.
// `initial` defines the initial value for the accumulation. Reference is an
// optional additional argument that make a common case -- comparing 2
// objects -- easy and more efficient.
function enumerate(object, next, initial, reference) {
var accumulated = initial;
for (var key in object)
// Test for own keys with `hasOwnProperty` instead of `Object.keys` to
// avoid garbage creation.
//
// https://en.wikipedia.org/wiki/Linked_list
// https://blog.jcoglan.com/2007/07/23/writing-a-linked-list-in-javascript/
// Reducible prototype for linked list node.
var __node__ = {
reduce: function reduceNodes(reducer, initial) {
var node = this;
var accumulated = initial;
do {
accumulated = reducer(accumulated, node);
@gordonbrander
gordonbrander / accumulators.js
Last active December 26, 2020 08:26
Accumulators -- a tiny JavaScript implementation of Clojure's reducers.
// Accumulators
// =============================================================================
//
// A tiny library for reactive programming that offers blazing fast generic
// collection manipulation, asyncronous flow control and the ability to
// represent infinitely large collections.
//
// Copyright Gordon Brander, 2013. Released under the terms of the [MIT license](http://opensource.org/licenses/MIT).
//
// Background:
@gordonbrander
gordonbrander / dispatcher.js
Created March 23, 2013 00:06
JavaScript multiple dispatch using predicate functions
var nil = 'indicating a nil value';
function reducePredicatePair(reduction, pair) {
// If no reduction has yet been found and the predicate
// matches, run the assocated function.
if(reduction[0] === nil && pair[0].apply(null, reduction[1])) {
reduction[0] = pair[1].apply(null, reduction[1]);
}
return reduction;
}