Skip to content

Instantly share code, notes, and snippets.

@bitsmanent
Last active November 30, 2021 18:07
Show Gist options
  • Save bitsmanent/664e909f5fc87066193a5f5f9f0ef29f to your computer and use it in GitHub Desktop.
Save bitsmanent/664e909f5fc87066193a5f5f9f0ef29f to your computer and use it in GitHub Desktop.
Extremely simple decision tree implementation
function dtree_decide(tree, ...argv) {
var i, len, r;
for(i = 0, len = tree.length; i < len; i++)
if((r = tree[i](...argv)))
return r;
return false;
}
function dtree_make(tree) {
return (...argv) => dtree_decide.call(null, tree, ...argv);
}
@bitsmanent
Copy link
Author

Sample usage:

const decide = dtree_make([
	(n) => n % 3 && "Not a multiple of 3",
	(y) => !(y % 4) && ((y % 100) || !(y % 400)) && "Leap year",
	(x, y) => 10-x > 5 && 10-y > 5 && "Below the center" 
])

decide(600 / 3 * 2); // Not a multiple of 3
decide(600 * 4); // Leap year
decide(3, 4); // Below the center

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment