Skip to content

Instantly share code, notes, and snippets.

View dherman's full-sized avatar

Dave Herman dherman

View GitHub Profile
@dherman
dherman / loop-controls.js
Created January 13, 2012 22:50
using loop controls from within block lambdas
Mailbox.prototype.extractContents = function(contacts) {
let messages = this.messages;
loop:
for (let i = 0, n = messages.length; i < n; i++) {
messages[i].headers.forEach { |header|
if (header.isSpam())
continue loop; // the label is not strictly necessary here
let addresses = header.extractAddresses();
addresses.forEach { |addr|
@dherman
dherman / unreturn.js
Created January 16, 2012 23:13
overriding return
function test() {
try {
try {
return 12
} finally {
throw "oh no you don't"
}
} catch (e) { }
console.log("ha!");
}
@dherman
dherman / main.js
Created January 22, 2012 22:31
require shim for Narcissus to run in SpiderMonkey
// main :: ([string], [string]) -> Narcissus
function main(moduleNames, moduleSources) {
var Narcissus = {__proto__: null};
// make the global object available to the interpreter via require("./global")
Narcissus.global = (new Function("return this"))();
moduleNames.forEach(function(name) {
Narcissus[name] = {};
});
@dherman
dherman / 1-recursive.js
Created February 7, 2012 20:10
turning recursion into iteration in JS
// Version 1. Simple recursive function. Blows the stack for large nodes.
function replace(node, from, to) {
switch (node.type) {
case IF:
return {
type: IF,
test: replace(node.test, from, to),
then: replace(node.then, from, to),
else: replace(node.else, from, to)
};
@dherman
dherman / or.ss
Created February 22, 2012 18:46
simple macro-by-example macro
(define-syntax or
(syntax-rules ()
[(or e)
e]
[(or e1 es ...)
(let ([x e1])
(if x x (or es ...)))]))
@dherman
dherman / short-functions.js
Created March 10, 2012 16:14
using -> for concise functions and => for TCP functions
// 1. Shorter function syntax.
//
// This version does not respect "Tennent's correspondence principle" -- it's nothing
// more than syntactic sugar for the traditional function syntax. That means when you
// see the normal braced body of a function, whether it's a longhand function or this
// shorthand version, there's no difference: return, this, and arguments are all tied
// to the new function body, and there's no implicit returning of the last expression
// result.
a.some((x) -> {
@dherman
dherman / Animal.java
Created March 21, 2012 16:40
super.methodCall() in Java
public class Animal {
public void foo() {
System.out.println("foo!");
}
public void bar() {
System.out.println("bar!");
}
}
@dherman
dherman / 1-before.js
Created April 16, 2012 02:12
refactoring hazard with missing semicolons
function mumble() {
foobar()
var x;
[1, 2, 3].forEach(quux);
if (blah()) {
x = "oogy";
} else {
x = "boogy";
}
return xyzzx(x);
@dherman
dherman / backticks.txt
Created April 23, 2012 17:05
ES6 template string syntax
console.log(`
Usage: node ${process.argv[1]} [OPTIONS]
-h print this usage information
-L launch the nuclear missiles
-k draw a kitty
`);
// including a backtick
console.log(`The backtick character: ${"`"}`);
@dherman
dherman / sm.js
Created May 9, 2012 06:10
sm.js with classes
/*
* Here is an implementation of an NPM module I recently wrote, done with ES6 classes.
* Is it a "betrayal" of the prototypal nature of JavaScript to use classes? Come on.
* Classes are a very common design pattern that's easy to implement with prototypes.
* And for some use cases, they're a good fit. Node's EventEmitter abstraction is a
* perfectly reasonable use of classes and inheritance: it's an abstract datatype that's
* user-extensible. So the 'events' module provides a base class with some built-in
* functionality that you can extend.
*
* And yes, it's a "class." Just check the docs: