Skip to content

Instantly share code, notes, and snippets.

View orrgal1's full-sized avatar

Or Gal orrgal1

View GitHub Profile
@orrgal1
orrgal1 / productable.js
Created December 31, 2018 13:44
Productable: a tiny function to create product of arrays
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]);
});
});
@orrgal1
orrgal1 / exec-decision-tree.js
Created January 15, 2019 08:49
Executable Decision Tree
// a work in progress...
const autoBind = require('auto-bind');
/**
* A Tree of decision making executable nodes.
*/
class Tree {
/**
* @constructor {String} name.
@orrgal1
orrgal1 / random-weighted-element.js
Created February 12, 2019 19:19
Efficient random element from weighted array
//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 });