Skip to content

Instantly share code, notes, and snippets.

@eplawless
Last active August 29, 2015 14:02
Show Gist options
  • Save eplawless/a4e727b5f8d29ae39e89 to your computer and use it in GitHub Desktop.
Save eplawless/a4e727b5f8d29ae39e89 to your computer and use it in GitHub Desktop.
selectListOfMostRecent
var Rx = require('rx');
(function(exports) {
function shiftLeft(list, value) {
var result = list.slice(1);
result.push(value);
return result;
}
exports.selectListOfMostRecent = function selectListOfMostRecent(count, defaultValue) {
count = Math.max(count, 1);
var listOfValues = [];
for (var idx = 0; idx < count; ++idx) {
listOfValues[idx] = defaultValue;
}
return this.scan(listOfValues, shiftLeft);
};
})(Rx.Observable.prototype);
function print() { return console.log.apply(console, arguments); }
function increment(value) { return ++value; }
Rx.Observable
.interval(800)
.scan(0, increment)
.selectListOfMostRecent(4, 0)
.subscribe(print);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment