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
// 1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. | |
// 2. Any live cell with more than three live neighbours dies, as if by overcrowding. | |
// 3. Any live cell with two or three live neighbours lives on to the next generation. | |
// 4. Any dead cell with exactly three live neighbours becomes a live cell. | |
type Grid = boolean[][]; | |
const getNextStateOfCell = (grid: Grid, [y, x]: [number, number]): boolean => { | |
const liveNeighbours = [ | |
grid[y - 1]?.[x - 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
const memo = (fn) => { | |
let lastArg; | |
return (arg) => { | |
if (typeof lastArg === 'undefined' || arg !== lastArg) fn((lastArg = arg)); | |
}; | |
}; | |
const decorateStorage = () => { | |
if (typeof Storage === 'undefined') { | |
throw new Error('No Storage found in environment'); |
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 partitioner = | |
<T extends any[]>(fn: (arg: T) => string) => | |
(arr: T) => | |
Object.values( | |
arr.reduce((acc, item) => { | |
const id = fn(item); | |
acc[id] = acc[id] || []; | |
acc[id].push(item); | |
return acc; | |
}, {}), |
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
import React, { useState } from 'react'; | |
const setupActions = (setState,actions) => () => Object.entries(actions).reduce((acc,[name,fn]) => ({...acc,[name]:(...args) => setState(fn(...args))}),{}); | |
export const statefulComponent = (initial,actions,stateProp='state') => Cmp => props => { | |
const [state,setState] = useState(initial); | |
return <Cmp | |
{...props} | |
{...{[stateProp]:state}} | |
{...useState(setupActions(setState,actions))[0]} |
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
var a = [ | |
1, | |
[1,2], | |
[1,[1,2,3]], | |
[1,[[1,2,3]]], | |
[[[[[[1,2,]],1,2]]],1,[1,2]] | |
]; | |
function flatten(arr) { | |
return arr.reduce(function(c, item) { |
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
var gulp = require('gulp'); | |
/* | |
Dependencies used to be loaded like this: (too slow) | |
var sass = require('gulp-sass'); | |
var cssShrink = require('gulp-cssshrink'); | |
var bower = require('gulp-bower'); | |
var uglify = require('gulp-uglify'); |