Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
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 / 1.PromiseQueue.ts
Last active July 28, 2024 13:17
A simple implementation of a promise queue with concurrency
import { deferred } from './deferred';
export class PromiseQueue<T> {
readonly concurrency: number;
#ongoingPromisesCount: number;
#toRun: Array<() => Promise<T>>;
constructor({ concurrency = 1 } = {}) {
this.concurrency = concurrency;
this.#ongoingPromisesCount = 0;
// 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
/**
* Backbone.Model shape propType checker for the `prop-types` package.
*/
import PropTypesSecret from 'prop-types/lib/ReactPropTypesSecret';
function createChainableTypeChecker(validate) {
function checkType(isRequired, props, propName, componentName, location, propFullName) {
var value = props[propName];
propFullName = propFullName || propName;
const State = state => new Proxy({
state,
listeners: [],
subscribe(listener) {
this.listeners.push(listener)
return () => this.listeners.filter(x => x !== listener)
},
set(stateUpdates) {
this.state = Object.assign({}, this.state, stateUpdates);
this.listeners.forEach(f => {