Skip to content

Instantly share code, notes, and snippets.

@gallettilance
Last active November 14, 2017 21:42
Show Gist options
  • Save gallettilance/03a84b33a28cfc56d3d4e217761c0ed5 to your computer and use it in GitHub Desktop.
Save gallettilance/03a84b33a28cfc56d3d4e217761c0ed5 to your computer and use it in GitHub Desktop.
Functional Reactive Programming
// // // // // // // // // // // // // // // //
// set up:
// you have a counter with id="theCounts_triples"
// and buttons for the prev points, next points
// and a reset button. We order the points by
// the sum of their coordinates (i.e. along the
// diagonally cutting plane
// // // // // // // // // // // // // // // //
var tripleNext = $('#triplesNext').asEventStream('click');
var triplePrev = $('#triplesPrev').asEventStream('click');
var reset = $('#resetTriples').asEventStream('click');
function aux2(n, i, j) {
if (i+j < n) {
return "( " + [i, j, n-(i+j)] + " )" + ", " + aux2(n, i+1, j)
} else {
if (j <= n) {
return "( " + [i, j, n-(i+j)] + " )" + ", " + aux2(n, 0, j+1)
} else {
return []
}
}
}
function triang(n) {
return aux2(n, 0, 0)
}
var merg = tripleNext.map(1).merge(triplePrev.map(-1)).merge(reset.map(0));
var next = merg.scan(0, function(x,y) {if (y === 0) {return 0} else {return x + y}});
var theCounts_triples = next.map(function(x) {return "( " + triang(x).toString().slice(0, -2) + " )"});
theCounts_triples.assign($('#theCounts_triples'), 'text');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment