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
type Point = { x: number; y: number }; | |
type Circle = Point & { radius: number; color: string }; | |
type StrokeCircle = Circle & { | |
width: number; | |
isDashed: boolean; | |
lineDash: number[]; | |
}; |
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
const between = (min: number, max: number, x: number) => | |
Math.max(min, Math.min(max, x)); | |
type Point = { x: number; y: number }; | |
type Direction = 'up' | 'down' | 'left' | 'right'; | |
export function* lazySpiralGenerator( | |
width: number, | |
height: number, |
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
const quickSort = (xs, compare = (a, b) => a - b) => | |
xs.length === 0 | |
? xs | |
: [ | |
...quickSort(xs.slice(1).filter(x => 0 < compare(xs[0], x))), | |
xs[0], | |
...quickSort(xs.slice(1).filter(x => 0 >= compare(xs[0], x))), | |
] |
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
/** | |
Composable RegExps | |
`r` is an implementation of a tag function to create regular expressions from | |
a template litteral. | |
# The why | |
If you find yourself repeating several times the same pattern from one RegExp | |
to another one (for example `/[a-zA-Z0-9]{2,54}/`), you probably want to put | |
it in a variable and define your other RexExps with it. The problem is, the |
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
// Shaping functions | |
float impulse(float k, float x){ | |
float h = k * x; | |
return h * exp(1.0 - h); | |
} | |
float parabola(float x, float k){ | |
return pow(4.0 * x * (1.0 - x), k); | |
} |
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
import { debounce } from 'lodash'; | |
class Deferred { | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { | |
this.resolve = resolve | |
this.reject = reject | |
}) | |
} | |
} |
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
export class PromiseQueue<T> { | |
readonly concurrency: number; | |
#ongoingPromisesCount: number; | |
#toRun: Array<() => Promise<T>>; | |
constructor({ concurrency = 1 } = {}) { | |
this.concurrency = concurrency; | |
this.#ongoingPromisesCount = 0; | |
this.#toRun = []; | |
} |
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
// Velocity based projection of position on | |
// UI with a fixed set of position a draggable element can take: | |
// for instance a grid where you can drag and drop items | |
const position = { | |
x: {value, 10, velocity: 2 }, | |
y: {value 10, velocity: 7 } | |
} | |
// 1. calculate the final position of your item in function of the momentum it has |
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
// safe objects won't throw when trying to get a | |
// property which doesn't exist. | |
const safe = value => new Proxy({ | |
extract() { | |
return value | |
} | |
}, { | |
get(obj, key) { | |
return key === 'extract' |
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
/* --------------------------------------------------------------------------- * | |
Recursive list utilities, taking advantage of Tail Call Elimination | |
* --------------------------------------------------------------------------- */ | |
// Tail call elimination is a technique used by modern browsers to optimize recursive | |
// functions. It's not yet implemented in every browsers but part of the ES6 specs. | |
// Basically, the browser gets rid of the call stack while a recursive function | |
// is still being executed. | |
// To make it possible for browsers to optimize such functions, we have to directly |