Last active
November 14, 2017 21:42
-
-
Save gallettilance/03a84b33a28cfc56d3d4e217761c0ed5 to your computer and use it in GitHub Desktop.
Functional Reactive Programming
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
// // // // // // // // // // // // // // // // | |
// 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