Created
August 18, 2016 09:40
-
-
Save Heimdell/69219df440a1648684dd8169cd39f3c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let {event, map, filter, accumulate, forEach} = require("./frp.js") | |
let {signal: time, feed} = event() | |
var encodedTime = map(time, x => new Date(x)) | |
var oddSeconds = filter(encodedTime, time => time.getSeconds() % 2 == 1) | |
var threeLastTimesWithOddSeconds = | |
accumulate(oddSeconds, [], (acc, time) => { | |
if (acc.length > 2) | |
acc.shift() | |
acc.push(time) | |
return acc | |
}) | |
forEach(threeLastTimesWithOddSeconds, array => { | |
console.log(array) | |
}) | |
console.log("Machinery is built, starting...") | |
for (var i = 0; i < 20; i++) { | |
setTimeout(() => { | |
feed(Date.now()) | |
}, i * 344) | |
} |
This file contains hidden or 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 event = () => { | |
var signal = {subscriptions: (x) => null} | |
var feed = (x) => signal.subscriptions(x) | |
return {signal, feed} | |
} | |
var subscribe = (signal, feed_transform) => { | |
let {signal: result, feed} = event() | |
var old = signal.subscriptions | |
signal.subscriptions = (x) => { | |
feed_transform(feed, x) | |
old(x) | |
} | |
return result | |
} | |
var map = (signal, f) => | |
subscribe(signal, (feed, x) => | |
feed(f(x)) | |
) | |
var filter = (signal, pred) => | |
subscribe(signal, (feed, x) => { | |
if (pred(x)) feed(x) | |
}) | |
var accumulate = (signal, zero, plus) => | |
subscribe(signal, (feed, x) => { | |
feed(zero = plus(zero, x)) | |
}) | |
var forEach = (signal, handler) => | |
subscribe(signal, (feed, x) => { | |
handler(x) | |
}) | |
module.exports = {event, map, filter, accumulate, forEach} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment