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:
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
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); |
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() { '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(); |
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 toObservable(asyncIter) { | |
return new Observable(sink => { | |
let abort = false; | |
(async _=> { | |
try { | |
for async (let x of asyncIter) { | |
if (abort) return; | |
sink.next(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
function toObservable(asyncIter) { | |
return new Observable(sink => { | |
let abort = false; | |
(async _=> { | |
try { | |
for async (let x of asyncIter) { | |
if (abort) return; | |
sink.next(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
async function* flattenStreams(asyncIterList) { | |
function deferred() { | |
let capability = {}; | |
capability.promise = new Promise((resolve, reject) => { | |
capability.resolve = resolve; | |
capability.reject = reject; | |
}); | |
return capability; | |
} |
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
Observable.from = function(iterable) { | |
return new Observable((push, error, close) => { | |
try { for (let x of iterable) push(x) } | |
catch (x) { error(x) } | |
finally { close() } | |
}); | |
}; |
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
const unwrapSymbol = Symbol(); | |
function Definitely(x) { | |
if (x == null || !(unwrapSymbol in x)) | |
return x; | |
return x[unwrapSymbol]; | |
} |
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
/* Redefine jQuery methods as "raw" methods */ | |
(function($) { "use strict"; | |
function wrap(method) { | |
return function() { | |
var jq = $(this), | |
result = jq[method].apply(jq, arguments); |
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($) { "use strict"; | |
function wrap(method) { | |
return function() { | |
var jq = $(this), | |
result = jq[method].apply(jq, arguments); | |
if (result && result.constructor === $) |