Skip to content

Instantly share code, notes, and snippets.

View dherman's full-sized avatar

Dave Herman dherman

View GitHub Profile
@dherman
dherman / var-function.js
Created October 9, 2011 18:51
Allowing keywords as variables creates cross-phase ambiguities
function wtf(x) {
var f =
function(x)
{
alert(x);
}
var function = alert;
}
@dherman
dherman / step1.js
Created October 16, 2011 03:23
deriving an implementation of call and bind functions in ES5
// *** Step 1a: method.call; must assume F.p.call not mutated
// app code:
var method = other.method;
method.call(self, arg0, arg1, ...);
// *** Step 1b: save .call up front; but still uses .call again!
// library code:
var callMethod = Function.prototype.call;
// app code:
var method = other.method;
@dherman
dherman / saxtest.js
Created October 26, 2011 18:23
sax cdata events
var sax = require("sax");
var fs = require("fs");
function getCdata(path, cb) {
var results = [];
var stream = fs.createReadStream(path);
var saxStream = sax.createStream(false, { lowercasetags: true });
var cdata;
saxStream.on("opencdata", function() {
console.log("OPEN CDATA");
// Demonstrating @jashkenas's examples in an even-more-minimalist version of
// classes that only allow object-literal syntax for the class body.
// Basic usage; unchanged.
class Color {
constructor: function(hex) {
...
},
@dherman
dherman / minimalist-classes.js
Created November 9, 2011 06:05 — forked from jashkenas/minimalist-classes.js
minimalist classes based on variable-declaration syntax
// Examples using something closer to my original "minimal classes" proposal.
// There are several benefits to this approach.
// First, it uses a familiar approach to separating prototype properties from
// instance properties: methods are on the prototype, and static declarations
// are prototype data properties. Otherwise, instance properties are only
// declared in the constructor body.
// Second, instead of object literal syntax, it uses a property declaration
@dherman
dherman / from.js
Created November 18, 2011 15:23
possible meanings of Array.from
Array.from = function from(x) {
var result = new Array();
for (var i = 0, n = x.length; i < n; i++) {
if (i in x)
result[i] = x[i];
}
return result;
};
// If Object.getPrototypeOf(Subarray) === Array, then:
@dherman
dherman / cascade.js
Created December 1, 2011 07:22
cascade syntax for JS
// inspired by https://github.com/raganwald/homoiconic/blob/master/2011/11/sans-titre.md#readme
// traditional method chaining with combinators:
console.log(range(1, 3)
.concat(range(4, 6))
.map(function(x) { return x * x })
.filter(function(x) { return x % 2 === 0 })
.reverse());
// method chaining with cascades:
@dherman
dherman / mustache-cascade.js
Created December 1, 2011 08:01
Trying out cascades similar to Bob's Dart proposal
// inspired by https://github.com/raganwald/homoiconic/blob/master/2011/11/sans-titre.md#readme
// and by https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/611c04100ac17142
// traditional method chaining with combinators:
console.log(range(1, 3)
.concat(range(4, 6))
.map(function(x) { return x * x })
.filter(function(x) { return x % 2 === 0 })
.reverse());
@dherman
dherman / monocle-lens.js
Created December 1, 2011 08:14
Variation on Bob's monocle-mustache, using parens instead of braces
// inspired by https://github.com/raganwald/homoiconic/blob/master/2011/11/sans-titre.md#readme
// and by https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/611c04100ac17142
// traditional method chaining with combinators:
console.log(range(1, 3)
.concat(range(4, 6))
.map(function(x) { return x * x })
.filter(function(x) { return x % 2 === 0 })
.reverse());
@dherman
dherman / monocle-mustache-semi.js
Created December 1, 2011 08:24
Monocle-mustache using semicolons (intuition: blocks and statements, not object literals)
// inspired by https://github.com/raganwald/homoiconic/blob/master/2011/11/sans-titre.md#readme
// and by https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/611c04100ac17142
// traditional method chaining with combinators:
console.log(range(1, 3)
.concat(range(4, 6))
.map(function(x) { return x * x })
.filter(function(x) { return x % 2 === 0 })
.reverse());