Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / derp-modules.js
Created October 9, 2012 22:06
Simple, tiny, dumb JavaScript Modules
// Simple, tiny, dumb module definitions for Browser JavaScript.
//
// What it does:
//
// * Tiny enough to include anywhere. Intended as a shim for delivering
// browser builds of your library to folks who don't want to use script loaders.
// * Exports modules to `__modules__`, a namespace on the global object.
// This is an improvement over typical browser code, which pollutes the
// global object.
// * Prettier and more robust than the
@gordonbrander
gordonbrander / idOf.js
Last active December 14, 2015 06:29 — forked from anonymous/idOf.js
function uid() {
// Generate a unique and non-colliding id.
return Math.random().toString(36).slice(2);
}
// Create unique, non-colliding ID namespace.
var __id__ = uid();
function idOf(thing) {
return (thing && typeof(thing) === ('object' || 'function')) ?
@gordonbrander
gordonbrander / example.js
Created March 1, 2013 17:51
updateState - a utility for representing/updating program state as an object.
var STATE_FILTERS = [
function clampIndex(state, old, update) {
return extend(state, {
// Validate and sanitize properties.
index: clamp(state.index, 0, state.units - 1)
});
},
function calculateOffset(state, old, update) {
return extend(state, {
// Create calculated properties.
@gordonbrander
gordonbrander / fps-reduce.js
Last active November 6, 2022 07:34
FPS Reduce -- a stream of times to use as an alternative to event loops.
var reducible = require("reducible/reducible");
var isReduced = require("reducible/is-reduced");
function fps(desiredFps) {
// Create a stream of times to use as an event loop with
// https://github.com/Gozala/coreduction/blob/master/coreduction.js
// Number -> Reducible[Float time, Float time, ...]
// Convert seconds to milliseconds.
var msPerFrame = 1000 / desiredFps
@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;
}
@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:
// 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 / 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.
//
@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 / 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])