Comparing version 0.5.1 to 0.5.2
{ | ||
"name": "most", | ||
"main": "Stream.js", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"homepage": "https://github.com/cujojs/most", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -9,2 +9,5 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var Yield = step.Yield; | ||
var End = step.End; | ||
exports.from = from; | ||
@@ -41,3 +44,3 @@ exports.head = head; | ||
return iteration.done ? iteration | ||
: new step.Yield(iteration.value, new IterableWrapper(iterator)); | ||
: new Yield(iteration.value, new IterableWrapper(iterator)); | ||
}); | ||
@@ -68,4 +71,5 @@ } | ||
ArrayIterator.prototype.next = function() { | ||
return this.index < this.array.length ? new step.Yield(this.array[this.index++]) | ||
: new step.End(); | ||
return this.index < this.array.length | ||
? new Yield(this.array[this.index++]) | ||
: new End(); | ||
}; |
@@ -7,15 +7,10 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
exports.Yield = Yield; | ||
exports.Skip = Skip; | ||
exports.End = End; | ||
function Yield(x, s) { | ||
this.done = false; this.skip = false; this.value = x; this.state = s; | ||
this.done = false; this.value = x; this.state = s; | ||
} | ||
function Skip(s) { | ||
this.done = false; this.skip = true; this.value = void 0; this.state = s; | ||
} | ||
function End(x) { | ||
this.done = true; this.skip = false; this.value = x; this.state = this; | ||
this.done = true; this.value = x; this.state = this; | ||
} |
{ | ||
"name": "most", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "Monadic streams", | ||
@@ -5,0 +5,0 @@ "main": "Stream.js", |
139
Stream.js
@@ -14,6 +14,5 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
/** @typedef {Yield|Skip|End} Step */ | ||
/** @typedef {Yield|End} Step */ | ||
var Yield = Stream.Yield = step.Yield; | ||
var Skip = Stream.Skip = step.Skip; | ||
var End = Stream.End = step.End; | ||
@@ -52,3 +51,3 @@ | ||
Stream.of = function(x) { | ||
return new Stream(identity, once(x)); | ||
return new Stream(identity, one(x)); | ||
}; | ||
@@ -71,3 +70,3 @@ | ||
Stream.fromPromise = function(p) { | ||
return new Stream(identity, p.then(once)); | ||
return new Stream(identity, p.then(one)); | ||
}; | ||
@@ -130,7 +129,7 @@ | ||
Stream.prototype.forEach = Stream.prototype.observe = function(f) { | ||
return runStream(f, this.step, this.state); | ||
return immediate(runStream, f, this.step, this.state); | ||
}; | ||
function runStream(f, stepper, state) { | ||
return next(stepper, state).then(function(s) { | ||
return when(next(stepper, state), function(s) { | ||
if (s.done) { | ||
@@ -140,3 +139,3 @@ return s.value; | ||
return Promise.resolve(f(s.value)).then(function (x) { | ||
return when(f(s.value), function (x) { | ||
return x instanceof End ? x.value | ||
@@ -158,3 +157,3 @@ : runStream(f, stepper, s.state); | ||
return new Stream(function(s) { | ||
return next(stepper, s.state).then(function(i) { | ||
return when(next(stepper, s.state), function(i) { | ||
return i.done ? i | ||
@@ -177,16 +176,21 @@ : delay(s.value, yieldPair(i, s.value), scheduler); | ||
return new Stream(function(s) { | ||
return next(stepper, s.state).then(function(i) { | ||
if(i.done) { | ||
return i; | ||
} | ||
var now = scheduler.now(); | ||
var end = s.value; | ||
return now > end ? yieldPair(i, now + period) : skipPair(i, end); | ||
}); | ||
return debounceNext(stepper, s, period, scheduler); | ||
}, new Pair(scheduler.now(), this.state)); | ||
}; | ||
function debounceNext(stepper, s, period, scheduler) { | ||
return when(next(stepper, s.state), function(i) { | ||
if(i.done) { | ||
return i; | ||
} | ||
var now = scheduler.now(); | ||
var end = s.value; | ||
return now > end ? yieldPair(i, now + period) | ||
: debounceNext(stepper, new Pair(end, i.state), period, scheduler); | ||
}); | ||
} | ||
/** | ||
* Functor: Transform each value in the stream by applying f to each | ||
* Transform each value in the stream by applying f to each | ||
* @param {function(*):*} f mapping function | ||
@@ -198,3 +202,3 @@ * @returns {Stream} stream containing items transformed by f | ||
return new Stream(function (state) { | ||
return next(stepper, state).then(function(i) { | ||
return when(next(stepper, state), function(i) { | ||
return i.done ? i | ||
@@ -220,5 +224,4 @@ : new Yield(f(i.value), i.state); | ||
/** | ||
* Applicative: Apply each function in this stream to each item in the | ||
* provides stream. This generates, in effect, a cross product. This | ||
* stream must contain only functions. | ||
* Assume this stream contains functions, and apply each function to each item | ||
* in the provided stream. This generates, in effect, a cross product. | ||
* @param {Stream} xs stream of items to which | ||
@@ -234,3 +237,3 @@ * @returns {Stream} stream containing the cross product of items | ||
/** | ||
* Chain: Map each value in the stream to a new stream, and emit its values | ||
* Map each value in the stream to a new stream, and emit its values | ||
* into the returned stream. | ||
@@ -240,3 +243,3 @@ * @param {function(x:*):Stream} f chaining function, must return a Stream | ||
*/ | ||
Stream.prototype.chain = function(f) { | ||
Stream.prototype.flatMap = Stream.prototype.chain = function(f) { | ||
return new Stream(stepChain, new Outer(f, this)); | ||
@@ -251,3 +254,3 @@ | ||
function stepOuter(stepChain, f, outer) { | ||
return streamNext(outer).then(function(i) { | ||
return when(Promise.resolve(streamNext(outer)), function(i) { | ||
return i.done ? i | ||
@@ -259,3 +262,3 @@ : stepInner(stepChain, f, new Stream(outer.step, i.state), f(i.value)); | ||
function stepInner(stepChain, f, outer, inner) { | ||
return streamNext(inner).then(function(ii) { | ||
return when(Promise.resolve(streamNext(inner)), function(ii) { | ||
return ii.done ? stepChain(new Outer(f, outer)) | ||
@@ -274,9 +277,13 @@ : new Yield(ii.value, new Inner(f, outer, new Stream(inner.step, ii.state))); | ||
return new Stream(function(state) { | ||
return next(stepper, state).then(function(i) { | ||
return i.done || p(i.value) ? i | ||
: new Skip(i.state); | ||
}); | ||
return filterNext(p, stepper, state); | ||
}, this.state); | ||
}; | ||
function filterNext(p, stepper, state) { | ||
return when(next(stepper, state), function(i) { | ||
return i.done || p(i.value) ? i | ||
: filterNext(p, stepper, i.state); | ||
}); | ||
} | ||
/** | ||
@@ -295,12 +302,17 @@ * Remove adjacent duplicates: [a,b,b,c,b] -> [a,b,c,b] | ||
return new Stream(function(s) { | ||
return next(stepper, s.state).then(function(i) { | ||
if(i.done) { | ||
return i; | ||
} | ||
return equals(s.value, i.value) ? skipPair(i, s.value) | ||
: yieldPair(i, i.value); | ||
}); | ||
return distinctNext(equals, stepper, s); | ||
}, new Pair({}, this.state)); | ||
}; | ||
function distinctNext(equals, stepper, s) { | ||
return when(next(stepper, s.state), function(i) { | ||
if(i.done) { | ||
return i; | ||
} | ||
return equals(s.value, i.value) | ||
? distinctNext(equals, stepper, new Pair(s.value, i.state)) | ||
: yieldPair(i, i.value); | ||
}); | ||
} | ||
/** | ||
@@ -310,3 +322,3 @@ * @returns {Promise} a promise for the first item in the stream | ||
Stream.prototype.head = function() { | ||
return next(this.step, this.state).then(getValueOrFail); | ||
return when(Promise.resolve(streamNext(this)), getValueOrFail); | ||
}; | ||
@@ -318,4 +330,3 @@ | ||
Stream.prototype.tail = function() { | ||
var state = next(this.step, this.state).then(getState); | ||
return new Stream(this.step, state); | ||
return new Stream(this.step, when(streamNext(this), getState)); | ||
}; | ||
@@ -331,3 +342,3 @@ | ||
return new Stream(function(s) { | ||
return next(stepper, s).then(function(i) { | ||
return when(next(stepper, s), function(i) { | ||
return i.done || p(i.value) ? i | ||
@@ -346,3 +357,3 @@ : new End(); | ||
return new Stream(function(s) { | ||
return next(stepper, s.state).then(function(i) { | ||
return when(next(stepper, s.state), function(i) { | ||
var remaining = s.value - 1; | ||
@@ -378,3 +389,3 @@ return i.done ? i | ||
Stream.prototype.concat = function(s) { | ||
return Stream.from([this, s]).chain(identity); | ||
return new Stream(identity, two(this, s)).chain(identity); | ||
}; | ||
@@ -392,3 +403,3 @@ | ||
return new Stream(function(s) { | ||
return next(stepper, s.state).then(function(i) { | ||
return when(next(stepper, s.state), function(i) { | ||
if(i.done) { | ||
@@ -413,11 +424,10 @@ return i; | ||
Stream.prototype.reduce = function(f, initial) { | ||
return reduce(f, initial, this.step, this.state); | ||
return immediate(reduce, f, initial, this.step, this.state); | ||
}; | ||
function reduce(f, z, stepper, state) { | ||
return next(stepper, state).then(function(i) { | ||
return i.done ? z | ||
: reduce(f, f(z, i.value), stepper, i.state); | ||
} | ||
); | ||
return when(next(stepper, state), function(i) { | ||
return i.done ? z | ||
: reduce(f, f(z, i.value), stepper, i.state); | ||
}); | ||
} | ||
@@ -428,5 +438,3 @@ | ||
function next(stepper, state) { | ||
return Promise.resolve(state).then(stepper).then(function(i) { | ||
return i.skip ? next(stepper, i.state) : i; | ||
}); | ||
return when(state, stepper); | ||
} | ||
@@ -449,6 +457,10 @@ | ||
function once(x) { | ||
function one(x) { | ||
return new Yield(x, new End()); | ||
} | ||
function two(x, y) { | ||
return new Yield(x, new Yield(y, new End())); | ||
} | ||
function repeat(x) { | ||
@@ -466,6 +478,2 @@ return new Yield(x, x); | ||
function skipPair(step, x) { | ||
return new Skip(new Pair(x, step.state)); | ||
} | ||
function Outer(f, outer) { | ||
@@ -493,1 +501,16 @@ this.f = f; this.outer = outer; this.inner = void 0; | ||
} | ||
function when(x, f) { | ||
return isPromise(x) ? x.then(f) : f(x); | ||
} | ||
function isPromise(x) { | ||
return x !== null && (typeof x === 'object' || typeof x === 'function' ) && typeof x.then === 'function'; | ||
} | ||
var slice = Array.prototype.slice; | ||
function immediate(f) { | ||
return Promise.resolve(slice.call(arguments, 1)).then(function(args) { | ||
return f.apply(void 0, args); | ||
}); | ||
} |
24299
733