Skip to content

Instantly share code, notes, and snippets.

function inherit(proto, literal) {
var result = Object.create(proto);
for (var prop in literal) {
if (literal.hasOwnProperty(prop)) {
result[prop] = literal[prop];
}
}
return result;
}
@j201
j201 / gist:6227909
Last active December 21, 2015 01:29
// JavaScript's strings aren't multiline by default
var poem = "old pond . . .
a frog leaps in
water’s sound";
// SyntaxError: unterminated string literal
// You can get multiline strings by escaping the newline...
var poem = "old pond . . . \
a frog leaps in \
water’s sound";
@j201
j201 / gist:7365644
Created November 8, 2013 03:05
Simple bag operations for Clojure.
; Clojure doesn't have bags (multi-sets) natively.
; Here are a few utility functions that let you use maps as bags
(defn bag-conj
"Adds an item to a bag"
([bag item]
(if-let [n (get bag item)]
(assoc bag item (inc n))
(assoc bag item 1)))
([bag item & items]
@j201
j201 / gist:7388824
Created November 9, 2013 19:23
Function to convert multiple Web Audio AudioNodes into one AudioNode.
// Wraps multiple audioNodes into one node
function wrapNodes(context, inputNodes, outputNodes) {
var wrapper = context.createGain();
inputNodes.forEach(function(node) {
wrapper.connect(node);
});
wrapper.connect = function(node) {
outputNodes.forEach(function(outputNode) {
outputNode.connect(node);
});
@j201
j201 / gist:7e2659ec29a4235ed993
Created June 6, 2014 03:03
Immutable OOP in Clojure
(defprotocol Shape
(perimeter [])
(draw [scene x y]))
(defrecord Square [s]
Shape
(perimeter []
(* s 4))
(draw [scene x y]
(draw-polygon scene
var marked = require('marked');
var highlight = require('my-code-highlighter');
exports.render = function(el, text) {
el.innerHTML = marked(text, { highlight: highlight });
};
@j201
j201 / gist:e04fde312c3be19b4f41
Created June 25, 2014 03:22
Subclassing arrays in JS with __proto__
subArrayProto = {
__proto__ : Array.prototype,
evenElements : function() {
return this.filter(function(el) {
return el % 2 === 0;
});
}
};
function makeSubArray() {
Account customer image