Skip to content

Instantly share code, notes, and snippets.

🚀

Gabriel Vergnaud gvergnaud

🚀
Block or report user

Report or block gvergnaud

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View quickSort.js
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 Mar 11, 2019
Composable RegExp in javascript using template literals.
View 01_regexp-tag.js
/**
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
View frag-shader-utilities.glsl
// 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 Aug 1, 2018
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.
View batch.js
import { debounce } from 'lodash';
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
}
}
@gvergnaud
gvergnaud / PromiseQueue.js
Last active Jul 27, 2018
A simple implementation of a promise queue with concurrency
View PromiseQueue.js
export default class PromiseQueue {
constructor({ concurrency = 1 } = {}) {
this.concurrency = concurrency;
this.ongoingPromisesCount = 0;
this.toRun = [];
}
// add :: (() -> Promise b a) -> Promise b a
add(getPromise) {
if (this.ongoingPromisesCount < this.concurrency) {
View apple-like-fluid-ui-animation.js
// 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
View safe-object.js
// 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'
View tail-call-optimisation-utilities.js
/* --------------------------------------------------------------------------- *
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
View backbone-model-shape-proptypes.js
/**
* 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;
View State.js
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 => {
You can’t perform that action at this time.