Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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.
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 / 1d-spring.js
Created June 3, 2014 20:31
1D Spring integration
function dampenedHookeForce(displacement, velocity, stiffness, damping) {
// Hooke's Law -- the basic spring force.
// <http://en.wikipedia.org/wiki/Hooke%27s_law>
//
// F = -kx
//
// Where:
// x is the vector displacement of the end of the spring from its equilibrium,
// k is a constant describing the tightness of the spring.
var hookeForce = -1 * (stiffness * displacement);
@gordonbrander
gordonbrander / kv.js
Last active May 4, 2023 22:31
KV: key-value iteration functions for JavaScript
// KV.js
// Hash iteration functions
// Set a value on an object at field, returning object.
function set(object, key, value) {
// Set value on object at key.
object[key] = value;
// Return object.
return object;
}
@gordonbrander
gordonbrander / foldable.litcoffee
Last active August 29, 2015 14:03
Foldable -- fold elements based on textContent matches

Basic stuff

Add an element to an indexed object, mutating it. Returns object.

add = (indexed, item) ->
  Array.prototype.push.call(indexed, item)
  indexed

Generic reduction over anything. This will come in handy for iterating

@gordonbrander
gordonbrander / foldable.js
Last active November 6, 2022 07:34
Foldable -- fold elements based on search strings in hash
// Foldable.js
// =============================================================================
//
// Fold DOM elements based on whether they match a query string.
//
// How:
//
// var foldableParagraphs = document.querySelectorAll('p.foldable');
// foldable(foldableParagraphs)
//
@gordonbrander
gordonbrander / route.js
Last active April 30, 2022 01:26
route.js: a history.pushState microrouter without the routing fluff.
// Microrouter based on history.pushState.
// All thrills, no frills.
// Usage:
//
// var h = urlstate(callback);
// h.push('#foo')
function urlstate(callback) {
// Since `history.pushState` doesn't fire `popstate`, we can use this function
// instead. Will update history state and call `callback` with currently
// showing `url`.
@gordonbrander
gordonbrander / indexesof.js
Created July 18, 2014 17:46
indexesOf: collect all indexes of pattern found in string.
// Returns an array of all indexes at which `pattern` can be found in `string`.
function indexesOf(string, pattern) {
var i = -1;
var indexes = [];
// We mutate `i` in place with the result of `indexOf`. We also use the last
// value of `i + 1` to continue seeking from. Any index found is pushed into
// the `indexes` array. If we ever get `-1` as the result of `indexOf`, we
// stop looping.
while((i = string.indexOf(pattern, i + 1)) !== -1) indexes.push(i);
return indexes;