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
/* --------------------------------------------------------------- * | |
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') |
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
/* --------------------------------------------------------------------------- * | |
Utilities for managing lists, implemented using only recursion | |
* --------------------------------------------------------------------------- */ | |
const head = ([x]) => x | |
const tail = ([x, ...rest]) => rest | |
const last = xs => xs[xs.length - 1] |
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
// 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) |
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 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 |
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 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) | |
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
/** | |
* 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; |
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 |
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
/** | |
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
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))), | |
] |