Skip to content

Instantly share code, notes, and snippets.

@oepn
oepn / quicksort.js
Created April 17, 2016 04:34
Haskell-inspired quicksort as an ES6 one-liner, because why not.
const quicksort = ([x, ...xs]) => typeof x === 'undefined' ? [] : [...quicksort(xs.filter(a => a <= x)), x, ...quicksort(xs.filter(a => a > x))];
@oepn
oepn / random-weighted-es6.js
Last active December 28, 2020 21:16 — forked from aesnyder/random-weighted.coffee
Weighted random value in ES6
// Defined first due to TDZ
let weighted = (...weightMap) => weightMap
.map(({0: value, 1: weight}) => new Array(weight).fill(value))
.reduce((acc, current) => [...acc, ...current]);
let random = (array) => array[Math.floor(Math.random() * array.length)];
// You can generate a weighted array