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
/* | |
This example uses an inner generator and skips over the first iteration in order to | |
prevent data loss which occurs on the first call to next. | |
*/ | |
import * as FS from "async-fs"; | |
export function readFile(path) { |
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 queueAlways() { | |
let x; | |
for (let i = 0; i < 100000; ++i) | |
x = await i; | |
} | |
async function queueIf() { |
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 === $) |
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
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
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
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
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); |
OlderNewer