This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test() { | |
try { | |
try { | |
return 12 | |
} finally { | |
throw "oh no you don't" | |
} | |
} catch (e) { } | |
console.log("ha!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] = {}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax or | |
(syntax-rules () | |
[(or e) | |
e] | |
[(or e1 es ...) | |
(let ([x e1]) | |
(if x x (or es ...)))])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Animal { | |
public void foo() { | |
System.out.println("foo!"); | |
} | |
public void bar() { | |
System.out.println("bar!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mumble() { | |
foobar() | |
var x; | |
[1, 2, 3].forEach(quux); | |
if (blah()) { | |
x = "oogy"; | |
} else { | |
x = "boogy"; | |
} | |
return xyzzx(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ${"`"}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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: |