Skip to content

Instantly share code, notes, and snippets.

@anler
Created April 29, 2016 16:34
Show Gist options
  • Save anler/db443b1588649987155651668a12550a to your computer and use it in GitHub Desktop.
Save anler/db443b1588649987155651668a12550a to your computer and use it in GitHub Desktop.
"Purely" functional observable in JavaScript
var ones$ = Repeat(1);
var naturals$ = foldPast(sum, 0, ones$)
applyEach(
n => console.log(n),
naturals$
)
// 1 2 3 4 5 ...
function sum(x, y) {
return x + y
}
function foldPast(operator, initial, s) {
return function(callback) {
s(function(x) {
initial += x
callback(initial)
})
}
}
function Repeat(x) {
return function(callback) {
setInterval(function() {
callback(x);
}, 3000);
}
}
function applyEach(f, s) {
s(f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment