Skip to content

Instantly share code, notes, and snippets.

View skiano's full-sized avatar

Greg Skiano skiano

View GitHub Profile
typography.inject(typePallette)
color.inject(colorPallette)
<TypographyStyles/>
// <Style>
// .heading1 {
// ...
// }
@skiano
skiano / stableMatch.js
Created April 12, 2017 21:32
Thinking about stable matching within a homogeneous group based on similarity
const diff = (a, b) => Math.abs(a - b);
const removeSelf = (items, self) => (id) => items[id] !== items[self];
const makeComparator = (items, target) => (a, b) =>
diff(items[target], items[b]) - diff(items[target], items[a]);
const getStableMatches = (items) => {
const matches = [];
const available = [...items.keys()];
const preferences = available.map(id =>
available.filter(removeSelf(items, id)).sort(makeComparator(items, id)));
@skiano
skiano / gridf.js
Last active December 17, 2017 20:37
2D letter grid compression
// TAKES A 2D ARRAY OF LETTERS AND COMPRESSES TO STRING
// for example ...
// compressGrid([
// ['A','A','A','B','B'],
// ['B','A','B','B','B'],
// ['B','A','B','B','B'],
// ['B','A','B','B','B'],
// ])
// becomes...
// 'A2B1,BAB2,2'
@skiano
skiano / cursor.js
Last active December 20, 2017 19:47
const createCursor = (directions, onMove) => {
const dirKeys = Object.keys(directions);
const cursor = {};
let location = 0;
cursor.max = 1000;
cursor.get = () => location;
@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,
@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 / 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])
// logs this pattern
// *
// * *
// * *
// * * * *
// * *
// * * * *
// * * * *
// * * * * * * * *
// 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'
@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;