Skip to content

Instantly share code, notes, and snippets.

Avatar

Or Gal orrgal1

View GitHub Profile
@orrgal1
orrgal1 / random-weighted-element.js
Created Feb 12, 2019
Efficient random element from weighted array
View random-weighted-element.js
//get a random weighted element in O(n) time and O(n) space
const randomWeightedElement = (elems, weightFn) => {
if(!elems || !elems.length) return;
const weighted = [];
let sum = 0;
for (let elem of elems) {
const weight = weightFn(elem);
if (weight > 0) {
sum += weight;
weighted.push({ elem, boundary: sum });
@orrgal1
orrgal1 / exec-decision-tree.js
Created Jan 15, 2019
Executable Decision Tree
View exec-decision-tree.js
// a work in progress...
const autoBind = require('auto-bind');
/**
* A Tree of decision making executable nodes.
*/
class Tree {
/**
* @constructor {String} name.
@orrgal1
orrgal1 / productable.js
Created Dec 31, 2018
Productable: a tiny function to create product of arrays
View productable.js
function Productable(arr) {
const self = {
array: arr.map(e => Array.isArray(e) ? e : [e]),
product: (otherArr) => {
const newArray = [];
self.array.forEach(e => {
otherArr.forEach(oe => {
newArray.push([...e, oe]);
});
});
You can’t perform that action at this time.