Skip to content

Instantly share code, notes, and snippets.

View dherman's full-sized avatar

Dave Herman dherman

View GitHub Profile
@dherman
dherman / 1.js
Created May 20, 2012 22:50
chaining and indentation
var users = records.map(function(record) {
return record.username;
})
.filter(function(username) { return !!username })
.map(function(username) {
return username.toLowerCase();
});
var image = new ParallelArray(canvas);
canvas.putImageData(image.map(toGrayScale).map(toPixel), 0, 0);
@dherman
dherman / cascade.js
Created June 1, 2012 20:57
deep vs shallow update
var obj = {
a: {
foo: 1,
bar: 2
},
b: {
quux: 3
},
c: 4
};
@dherman
dherman / map-dict.js
Created June 9, 2012 02:42
Map vs object for dictionary
// A Dict class that works in ES6 using Map. Basically it's identical to Map, but
// only expected to be used on string keys.
function Dict() {
this.elts = new Map();
}
// (string) -> any
Dict.prototype.get = function get(key) {
return this.elts.get(key);
@dherman
dherman / nbody.rs
Created June 14, 2012 05:30 — forked from pcwalton/gist:2927607
Rust of Patrick's dreams, with Dave's method syntax :)
import std.math.{PI, sqrt};
struct Body {
x: f64;
y: f64;
z: f64;
vx: f64;
vy: f64;
vz: f64;
mass: f64;
// This isn't a proposal for JS. It's just a little thought experiment:
//
// What if I could go back in time and redesign the semicolon insertion rules?
//
// One of the most annoying things about ASI is when it *doesn't* kick in because
// the following line starts with ( or [. The Go language has chosen a different
// policy, inserting semicolons even when the following line starts with ( or [.
// So in JS, when you see:
f()
@dherman
dherman / test.html
Created July 15, 2012 21:13
canvas line connecting compat issue
<!doctype html>
<head>
<title>Canvas Test</title>
<style>
#c {
border: solid 1px black;
}
</style>
</head>
<body>
@dherman
dherman / closure.js
Created July 27, 2012 21:39
Analog to Function constructor for building closures
(function() {
var hasOwnProperty = Object.prototype.hasOwnProperty;
var Function = hasOwnProperty.constructor;
function isIdentifier(s) {
return /[a-zA-Z_$][a-zA-Z_$0-9]*/.test(s);
}
@dherman
dherman / unselfish.js
Created July 28, 2012 18:08
Convert methods to unselfish functions
Function.prototype.unselfish = (function(Fp, Ap) {
var applyMethod = Fp.apply,
callMethod = Fp.call;
var apply = callMethod.bind(applyMethod),
call = callMethod.bind(callMethod);
var sliceMethod = Ap.slice;
return function unselfish() {
@dherman
dherman / beget.js
Created July 28, 2012 18:13
What everyone kinda wishes Object.create had been
Object.beget = (function() {
var create = Object.create,
defineProperty = Object.defineProperty;
// see https://gist.github.com/3194222
var hasOwn = Object.prototype.hasOwnProperty.unselfish();
return function beget(proto, own) {
var result = create(proto);