Skip to content

Instantly share code, notes, and snippets.

(require '[clojure.zip :as z])
; make a tree using vectors
(def data [1 [2 3]]) ;
data ; => [1 [2 3]]
; make a zipped version
(def dz (z/vector-zip data))
dz ; => [[1 [2 3]] nil] (nil for where the cursor data will be; but we haven't traversed anywhere)
var unnamed = function(){
var fns = [].slice.call(arguments);
return function(){
var args = [].slice.call(arguments);
return fns.map(function(fn){ return fn.apply(null, args) })
}
}
@hughfdjackson
hughfdjackson / reducify.js
Created October 10, 2012 16:35
reducify
/*
turns a binary function into a variadic function via reduction
*/
var reducify = function(fn){
return function(){
var args = [].slice.call(arguments)
return args.reduce(fn)
}
var bundle = function(){
var fns = [].slice.call(arguments)
return function(){
var args = [].slice.call(arguments)
fns.forEach(function(fn){ fn.apply(null, args) })
}
}
var f1 = function(a, b){ console.log(a + b) }
@hughfdjackson
hughfdjackson / gist:3119384
Created July 16, 2012 00:34
Ugly curry adaptation, for handling `new`
Function.prototype.curry = function () {
var slice = Array.prototype.slice,
args = slice.apply(arguments),
that = this;
return function _curried() {
var fn = Function.prototype.bind.apply(that, args.concat(slice.apply(arguments)));
fn.prototype = that.prototype;
if ( this instanceof _curried ) return new fn();
else return fn();
function deepClone(o) {
return (typeof o !== 'object' || o === null) ? o
: (o instanceof Array) ? o.map(deepClone)
: Object.keys(o).reduce(function (result, key) {
result[key] = deepClone(o[key])
return result
}, Object.create(Object.getPrototypeOf(o))) }
@hughfdjackson
hughfdjackson / gist:3053556
Created July 5, 2012 13:04
navigating by a string w/ dot syntax
var dotGet = function(o, propStr){
var getProp = function(o, name){
return ( typeof o == 'object' && o != null && name in o ) ? o[name]: undefined
}
return propStr.split('.').reduce(getProp, o)
}
// tests
var o = { x: { y: { z: 'test', bar: null } } }
@hughfdjackson
hughfdjackson / gist:2969479
Created June 22, 2012 00:27
LucidJS example
/*
LucidJS is a lib with a .emitter constructor function (one that doesn't require the new keyword; what i
tend to call informal constructor functions). It provides an object with the normal pub/sub methods; .on,
.off and .trigger
*/
var player = _.extend({
x: 0
, y: 0
, move: function(x, y){
function attachEvent(el, action, callback){
if (el.addEventListener) {
el.addEventListener(action, callback, false);
} else if (el.attachEvent) {
el.attachEvent('on' + action, (function (ele) { return callback; })(el) );
}
}
inputKeyup: function (courseType){//event that's triggered on the input keyups
var currentRequest;
return function (e){