Skip to content

Instantly share code, notes, and snippets.

@zenparsing
zenparsing / my-array-with-privates.js
Last active September 17, 2015 16:14
Self Hosted Array Supporting Private State
class X {
constructor(len) {
let target = Object.create(new.target.prototype);
let handler = {
// handler stuff - special logic for "length" and integer properties
// Otherwise, use target's properties
};
return Reflect.construct(Proxy, [target, handler], new.target);
@zenparsing
zenparsing / consuming-observable-with-generator.js
Created September 10, 2015 15:12
Consuming Observables with a Generator
(function() { 'use strict';
function consume(observable, gen) {
return new Promise((resolve, reject) => {
if (typeof gen === "function") {
// Prime the generator to accept values
gen = gen();
gen.next();
@zenparsing
zenparsing / flip-it-and-reverse-it.js
Created May 28, 2015 13:51
Flip It and Reverse It
function toObservable(asyncIter) {
return new Observable(sink => {
let abort = false;
(async _=> {
try {
for async (let x of asyncIter) {
if (abort) return;
sink.next(x);
@zenparsing
zenparsing / toObservable.js
Created May 28, 2015 13:42
Async Iterator to Observable
function toObservable(asyncIter) {
return new Observable(sink => {
let abort = false;
(async _=> {
try {
for async (let x of asyncIter) {
if (abort) return;
sink.next(x);
@zenparsing
zenparsing / async-iterators.js
Last active August 29, 2015 14:22
mergeStreams with Async Iterators and Observables
async function* flattenStreams(asyncIterList) {
function deferred() {
let capability = {};
capability.promise = new Promise((resolve, reject) => {
capability.resolve = resolve;
capability.reject = reject;
});
return capability;
}
@zenparsing
zenparsing / observable-scheduling.js
Last active August 29, 2015 14:20
Scheduling Observable using Promises
Observable.from = function(iterable) {
return new Observable((push, error, close) => {
try { for (let x of iterable) push(x) }
catch (x) { error(x) }
finally { close() }
});
};
@zenparsing
zenparsing / definitely-maybe.js
Last active August 29, 2015 14:19
Definitely Maybe with Proxy
const unwrapSymbol = Symbol();
function Definitely(x) {
if (x == null || !(unwrapSymbol in x))
return x;
return x[unwrapSymbol];
}
@zenparsing
zenparsing / jake.js
Last active August 29, 2015 14:09
jQuery Reimagined
/* Redefine jQuery methods as "raw" methods */
(function($) { "use strict";
function wrap(method) {
return function() {
var jq = $(this),
result = jq[method].apply(jq, arguments);
@zenparsing
zenparsing / jquery-direct.js
Last active August 29, 2015 14:09
jquery direct
(function($) { "use strict";
function wrap(method) {
return function() {
var jq = $(this),
result = jq[method].apply(jq, arguments);
if (result && result.constructor === $)
@zenparsing
zenparsing / abstract-references.md
Last active August 29, 2015 14:07
Abstract References for ECMAScript

Abstract References for ECMAScript

Overview and Motivation

Despite its functional programming facilities, ECMAScript exhibits a strong preference for object-oriented "left-to-right" composition using instance methods. Unfortunately, composition by means of instance methods requires that extensibility be achieved by adding function-valued properties to the object or a prototype ancestor of the object. This leads to the following difficulties: