Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
/* --------------------------------------------------------------- *
Remove node modules recursively in a pure functionnal way
* ---------------------------------------------------------------- */
// applying functionnal techniques from @drboolean in his egghead course
// https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript
const fs = require('fs.extra')
const path = require('path')
/* --------------------------------------------------------------------------- *
Utilities for managing lists, implemented using only recursion
* --------------------------------------------------------------------------- */
const head = ([x]) => x
const tail = ([x, ...rest]) => rest
const last = xs => xs[xs.length - 1]
// crack the ceasar code by minimazing the distance between english language letter
// occurrence and all the letter occurrence of all the possible shifts of a message.
const englishLetterOccurrence = [ 8.2, 1.5, 2.8, 4.3, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2, 0.8, 4.0, 2.4, 6.7, 7.5, 1.9, 0.1, 6.0, 6.3, 9.1, 2.8, 1.0, 2.4, 0.2, 2.0, 0.1 ]
const letters = 'abcdefghijklmnopqrstuvwxyz'.split('')
const letterOccurrence = str =>
letters
.map(c => (str.match(new RegExp(c, 'gi')) || []).length)
const Task = require('data.task')
const Either = require('data.either')
const { Left, Right } = Either
const request = require('request')
const { List } = require('immutable-ext')
const { drop, prop, map } = require('lodash/fp')
const effect = f => x => {
f(x)
return x
const stateIdLens = propLens('id')
const stateTokenLens = propLens('token')
let setCurrentUserId = (state, action) => set(idLens, action.payload.result, state)
let setCurrentUserToken = (state, action) => set(tokenLens, action.payload.token, state)
compose(setCurrentUserId, setCurrentUserToken)
/**
* 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;
/* --------------------------------------------------------------------------- *
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
// 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
@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
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))),
]