Skip to content

Instantly share code, notes, and snippets.

@bterlson
bterlson / spec.html
Last active August 29, 2015 13:59
Canonical Spec Section
<es-intro>
<p>Introductory matter. Could probably be an intro attribute on clauses</p>
</es-intro>
<es-clause title="Greetings" anchor="sec-greetings">
<!-- anchor is optional, is calculated for you if you don't specify -->
<p>Clauses are normative and contain text...</p>
<es-clause title="sub-clause">
<p>When evaluating <es-nt>BindingElement</es-nt>, you better do it right!</p>
class Cancellation extends Error { };
class CancellablePromise extends Promise {
constructor(resolver, onCancelled) {
this._onCancelled = onCancelled;
this._isResolved = false;
super((resolve, reject) => {
this._reject = reject;
const wrappedResolve = value => { resolve(value); this._isResolved = true; };
const wrappedReject = reason => { reject(reason); this._isResolved = true; };
class A {
if (defineBar) {
bar => {
}
}
while(needsMoreMethods) {
// add more methods
}
@bterlson
bterlson / stream-bifurcate.js
Created October 28, 2014 23:33
stream-bifurcate
var _ = require('highland');
var Readable = require('stream').Readable;
var Writable = require('stream').Writable;
var util = require('util');
util.inherits(Tributary, Writable);
function Tributary() {
Writable.call(this, { objectMode: true });
this.forks = [];
@bterlson
bterlson / oddlists
Created February 7, 2015 21:35
Copy the md syntax into the comment field, and hit preview.
1. L0
1. L1
1. L2
1. L2
1. L3
1. here
2. here
3. here
@bterlson
bterlson / concat.js
Last active August 29, 2015 14:19
Chained tagged templates
function concat(head, template) {
"use strict";
var head;
if(!template) {
template = head;
head = '';
}
var fn = concat.bind(null, head + template[0]);
@bterlson
bterlson / example.js
Created June 5, 2015 19:04
Async constructors?
async function Person() {
this.name = await getName();
}
// seems weird-ish.
var p = new Person();
p.then(person => console.log(person.name));
// but seems better in another async function
async function CreatePerson() {
@bterlson
bterlson / classy.js
Last active September 16, 2015 17:11
class A { }
class B extends A {
method() { super.x = 1; return super.x; }
}
var b = new B();
b.method(); // undefined (no property x on A.prototype or above)
b.hasOwnProperty('x'); // true (own property created on receiver by [[set]])
class C extends B {
get x() { return "gotten" }
foo bar 123

Pattern Matching

This is a strawman proposal for adding pattern matching to ECMAScript. Pattern matching is useful for matching a value to some structure in a similar way to destructuring. The primary difference between destructuring and pattern matching are the use cases involved - destructuring is useful for binding pieces out of larger structures whereas pattern matching is useful for mapping a value's structure to data or a set of behaviors. In practice this means that destructuring tends to allow many shapes of data and will do its best to bind something out of it, whereas pattern matching will tend to be more conservative.

Additionally, the power of pattern matching is increased substantially when values are allowed to participate in the pattern matching semantics as a matcher as well as a matchee. This proposal includes the notion of a pattern matching protocol - a symbol method that can be implemented by objects that enables developers to use those values in pattern matching. A common scenario w