Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
@gvergnaud
gvergnaud / canvas.ts
Last active April 13, 2020 09:39
Some useful canvas utility functions to draw circles, lines and texts
type Point = { x: number; y: number };
type Circle = Point & { radius: number; color: string };
type StrokeCircle = Circle & {
width: number;
isDashed: boolean;
lineDash: number[];
};
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,
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))),
]
@gvergnaud
gvergnaud / 01_regexp-tag.js
Last active March 11, 2019 09:18
Composable RegExp in javascript using template literals.
/**
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
// 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);
}
@gvergnaud
gvergnaud / batch.js
Last active February 27, 2020 22:34
A `batch` function to batch several IOs together to gain in performance with the same api as if you were doing one IO at a time.
import { debounce } from 'lodash';
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
}
}
@gvergnaud
gvergnaud / PromiseQueue.ts
Last active March 31, 2024 14:19
A simple implementation of a promise queue with concurrency
export class PromiseQueue<T> {
readonly concurrency: number;
#ongoingPromisesCount: number;
#toRun: Array<() => Promise<T>>;
constructor({ concurrency = 1 } = {}) {
this.concurrency = concurrency;
this.#ongoingPromisesCount = 0;
this.#toRun = [];
}
// 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
// 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'
/* --------------------------------------------------------------------------- *
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