Skip to content

Instantly share code, notes, and snippets.

View kevincennis's full-sized avatar
💭
this is dumb

Kevin Ennis kevincennis

💭
this is dumb
View GitHub Profile
@kevincennis
kevincennis / composition.js
Last active August 28, 2015 07:28
composition
function compose() {
var fns = Array.prototype.slice.call( arguments );
return function( arg ) {
return fns.reduceRight(function( result, fn ) {
return fn( result );
}, arg );
};
}
function double( n ) {
@kevincennis
kevincennis / mergebuffers.js
Last active August 29, 2015 13:56
Merge buffers
// assuming `rec1` and `rec2` are both instances of Recorder...
var buf1L, buf1R, buf2L, buf2R, worker, count = 0;
// start a new worker
// we can't use Recorder directly, since it doesn't support what we're trying to do
worker = new Worker('recorderWorker.js');
// initialize the new worker
worker.postMessage({
@kevincennis
kevincennis / tmpl.js
Last active August 29, 2015 14:05
tmpl.js
// KEWL TEMPLATING LIBRARY
function tmpl( fn ) {
return function( data ) {
return fn( data );
};
}
// DEMO...
@kevincennis
kevincennis / index.js
Last active August 29, 2015 14:11
"Native" YAML `require()`
// needs to be required once per process to set up the hook
require('./yaml.js');
// now i can require a yml file. yay!
console.log( require('./test') );
@kevincennis
kevincennis / curry.js
Last active August 29, 2015 14:14
curry
Function.prototype.curry = function curry() {
var fn = this,
arity = fn.length,
slice = Array.prototype.slice,
args = slice.call( arguments );
function accumulator() {
var locArgs = args;
if ( arguments.length > 0 ) {
@kevincennis
kevincennis / privacy.js
Created February 4, 2015 04:58
WeakMap privacy
var User = (function() {
var cache = new WeakMap();
function User( name ) {
cache.set( this, {} );
this.name = name;
}
User.prototype = {
@kevincennis
kevincennis / EventEmitter.js
Last active August 29, 2015 14:14
EventEmitter
var EventEmitter = (function() {
var cache = new WeakMap();
function EventEmitter() {
cache.set( this, {} );
}
EventEmitter.prototype = {
@kevincennis
kevincennis / EventEmitter.js
Last active August 29, 2015 14:14
Proxy Model
var EventEmitter = (function() {
var cache = new WeakMap();
function EventEmitter( obj ) {
cache.set( this, {} );
}
EventEmitter.prototype = {
function factorial( n ) {
if ( n === 1 ) {
return 1;
}
return n * factorial( n - 1 );
}
@kevincennis
kevincennis / map.js
Last active August 29, 2015 14:25
map
function map( arr, fn ) {
if ( arr.length === 0 ) {
return [];
}
return [ fn( arr[ 0 ] ) ].concat( map( arr.slice( 1 ), fn ) );
}