Skip to content

Instantly share code, notes, and snippets.

@phadej
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phadej/cb3d2bb6cd5bd793f073 to your computer and use it in GitHub Desktop.
Save phadej/cb3d2bb6cd5bd793f073 to your computer and use it in GitHub Desktop.
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