Created
April 29, 2016 16:34
-
-
Save anler/db443b1588649987155651668a12550a to your computer and use it in GitHub Desktop.
"Purely" functional observable in JavaScript
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 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