Last active
August 29, 2015 14:02
-
-
Save eplawless/a4e727b5f8d29ae39e89 to your computer and use it in GitHub Desktop.
selectListOfMostRecent
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
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