This file contains hidden or 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
const [head, ...tail] = [1,2,3]; | |
// head 1 | |
// tail [2,3] |
This file contains hidden or 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
// haskell | |
// sort [] = [] | |
// sort (x:xs) = sort lte ++ [x] ++ sort gt | |
// where lte = [a | a <- xs, a <= x] | |
// gt = [a | a <- xs, a > x] | |
const data = [4,5,8,9,1,2,3,9,1,5,1,9,5,3,3,3,8,9]; | |
function sort(data) { | |
if (_.isEmpty(data)) return []; |
This file contains hidden or 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
const fs = require('fs'); | |
const gm = require('gm'); | |
function getSize(image) { | |
return new Promise((resolve, reject) => { | |
image.size((err, size) => { | |
if (err) { | |
reject(err); | |
return; | |
} |
This file contains hidden or 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
const _ = require('lodash'); | |
function supersetInner(src) { | |
if (src.length === 0) return []; | |
const withouts = _.map(src, elem => superset(_.without(src, elem))); | |
return [src, ..._.flatten(withouts)]; | |
} | |
function superset(src) { |
This file contains hidden or 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
// modification version | |
function once(f) { | |
let hasRun = false; | |
let result; | |
return () => { | |
if (!hasRun) { | |
result = f(); | |
hasRun = true; | |
} | |
return result; |
This file contains hidden or 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
const diceTriplets = sides => | |
_(sides) | |
.range() | |
.flatMap(a => | |
_(sides) | |
.range() | |
.flatMap(b => | |
_(sides) | |
.range() | |
.map(c => [a + 1, b + 1, c + 1]) |
This file contains hidden or 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
_.reduce( | |
([numItemsSeen, mean], num) => [ | |
numItemsSeen + 1, | |
mean + (num - mean) / (numItemsSeen + 1) | |
], | |
[0, 0] | |
)([3, 4, 5, 6, 10, 100, 9]); |
This file contains hidden or 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
const a = [ | |
{ a: 'one', b: 4 }, | |
{ a: 'two', b: 1 }, | |
{ a: 'three', b: 4 }, | |
{ a: 'four', b: 2 } | |
]; | |
function argmaxAll(getterFunction, arr) { | |
return reduce( | |
([max, arr], curr) => { |
This file contains hidden or 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
const times = require('lodash/fp/times'); | |
const map = require('lodash/fp/map'); | |
const sample = require('lodash/fp/sample'); | |
const maxBy = require('lodash/fp/maxBy'); | |
const find = require('lodash/fp/find'); | |
const join = require('lodash/fp/join'); | |
const flow = require('lodash/fp/flow'); | |
const get = require('lodash/fp/get'); | |
const sum = require('lodash/fp/sum'); | |
const constant = require('lodash/fp/constant'); |
This file contains hidden or 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
shuffle_array = arr => | |
arr | |
.map(a => [a, Math.random()]) | |
.sort(([, lr], [, rr]) => lr - rr) | |
.map(([a]) => a); |