Skip to content

Instantly share code, notes, and snippets.

View skiano's full-sized avatar

Greg Skiano skiano

View GitHub Profile
@skiano
skiano / tag.js
Created August 11, 2022 22:51
A helper for creating dom stuff
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]);
}
}
@skiano
skiano / traverse.js
Created February 9, 2022 05:36
gen traverse but skip dulicates!
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) {
@skiano
skiano / linked.js
Last active September 28, 2018 02:46
little doubly linked list
function createList() {
var next = 'prev'
, prev = 'next'
, length = 0
, head
, tail
, arr
, n;
return {
// SOLUTION 1
const getBlobBoundingBox1 = (blobString) => {
blobString = blobString.trim()
const width = (/\s/.exec(blobString) || {}).index
const one = /1/g
blobString = blobString.replace(/\s+/g, '')
@skiano
skiano / interview-snippets.js
Last active March 8, 2018 15:35
Interesting code snippets to talk about
// -------------------------------------------------------------------------------------------------------
// 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;
// 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'
// logs this pattern
// *
// * *
// * *
// * * * *
// * *
// * * * *
// * * * *
// * * * * * * * *
@skiano
skiano / n-distance.js
Last active December 23, 2017 09:08
n-dimension distance
// 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])
@skiano
skiano / spiralGridWalk.js
Created December 22, 2017 23:47
walk the coordinates of a grid in a spiral to the center
// 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]
@skiano
skiano / fillOrder.js
Created December 22, 2017 23:03
establish an order for filling a grid
// >>> 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,