-
-
Save phadej/cb3d2bb6cd5bd793f073 to your computer and use it in GitHub Desktop.
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 Bacon = require("baconjs"); | |
var _ = require("lodash"); | |
// :: number -> number | |
var f = function (x) { return Math.cos(x); }; | |
// :: number | |
var threshold = 1e-5; | |
// :: number -> number | |
var round = function (x) { return Math.round(x * 1e4) * 1e-4; }; | |
// :: Bus number | |
var input = new Bacon.Bus(); | |
// :: EventSource { curr : number, next : number, cond : bool } | |
var tmp = input.map(function (x) { | |
var next = f(x); | |
return { curr: x, next: next, cond: Math.abs(x - next) < threshold }; | |
}); | |
// :: Property number | |
var out = tmp.filter(".cond").map(".next").map(round).skipDuplicates().toProperty(); | |
// :: EventSource number | |
var feedback = tmp.filter(function (p) { | |
return !p.cond; | |
}).map(".next").delay(0); // this delay is important! | |
input.plug(feedback); | |
out.log("out"); | |
input.log("in "); | |
input.push(1); | |
input.push(-1); | |
input.push(0.7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment