This file contains 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
function create(tag, attr = {}, children = []) { | |
children = Array.isArray(children) ? children : [children]; | |
const elm = document.createElement(tag); | |
for (let a in attr) { | |
if (a.startsWith('on')) { | |
elm.addEventListener(a.slice(2).toLowerCase(), attr[a]) | |
} else { | |
if (attr[a]) elm.setAttribute(a, attr[a]); | |
} | |
} |
This file contains 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
function* each(arr) { | |
let i; | |
for (i = 0; i < arr.length; i++) yield arr[i]; | |
} | |
export function* traverse (arr) { | |
let queue = [each(arr)]; | |
let seen = new WeakSet(); | |
while (queue.length) { |
This file contains 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
function createList() { | |
var next = 'prev' | |
, prev = 'next' | |
, length = 0 | |
, head | |
, tail | |
, arr | |
, n; | |
return { |
This file contains 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
// SOLUTION 1 | |
const getBlobBoundingBox1 = (blobString) => { | |
blobString = blobString.trim() | |
const width = (/\s/.exec(blobString) || {}).index | |
const one = /1/g | |
blobString = blobString.replace(/\s+/g, '') |
This file contains 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
// ------------------------------------------------------------------------------------------------------- | |
// Once (from source of async) | |
// @see https://github.com/caolan/async/blob/f5d86b80b986c8cad88a224a6f8b3ec154839490/lib/internal/once.js | |
// ------------------------------------------------------------------------------------------------------- | |
function once(fn) { | |
return function () { | |
if (fn === null) return; | |
var callFn = fn; | |
fn = null; |
This file contains 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
// A compressor for anagram puzzle data | |
// because we know all the solutions to anagram puzzles | |
// have a very small number of characters, we can compress | |
// the data more strategically | |
// | |
// given a list of letters, this creates an encode and decode function | |
// | |
// for example | |
// given the letters: | |
// 'NCEHITK' |
This file contains 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
// logs this pattern | |
// * | |
// * * | |
// * * | |
// * * * * | |
// * * | |
// * * * * | |
// * * * * | |
// * * * * * * * * |
This file contains 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
// not really about performance | |
// but fun to make it one line | |
// this discussion looked interesting re estimating | |
// https://stackoverflow.com/questions/3693514/very-fast-3d-distance-check | |
const distance = (a, b) => Math.sqrt(a.reduce((total, _, i) => total + Math.pow(b[i] - a[i], 2), 0)) | |
const _2d_ = distance([1, 2], [0, 4]) | |
const _3d_ = distance([1, 2, 3], [0, 4, 5]) |
This file contains 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
// walk around the edges of a grid | |
// and work toward the center | |
// Begin going right. | |
// When you are at an edge or a filled square, turn right. | |
const spiral = (w, h, fn) => { | |
// a map where keys are visited indexes | |
const visited = {} | |
// translations represent increments for: right -> down -> left -> up | |
const translations = [1, w, -1, -w] |
This file contains 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
// >>> is perf hack for flooring positive numbers | |
const rand = (min, max) => ((Math.random() * (max - min + 1)) >>> 0) + min | |
const randItem = (arr) => arr[rand(0, arr.length - 1)] | |
const isDef = v => typeof v !== 'undefined' | |
const isUndef = v => typeof v === 'undefined' | |
const directions = [ | |
(w, h, x, y, i) => y > 0 ? i - w : undefined, | |
(w, h, x, y, i) => y < h - 1 ? i + w : undefined, |
NewerOlder