Skip to content

Instantly share code, notes, and snippets.

View mcsf's full-sized avatar

Miguel Fonseca mcsf

View GitHub Profile
@mcsf
mcsf / deferred.js
Last active August 29, 2015 14:22
defer + promises
const log = console.log.bind(console, '--> ');
const warn = console.warn.bind(console, '!!! ');
export default function deferred(fn) {
return new Promise((res, rej) => {
setTimeout(() => {
try {
res(fn());
} catch (e) {
rej(e);
@mcsf
mcsf / timed.js
Last active August 29, 2015 14:25
Timing async calls with promises
const now = () => new Date().getTime();
const timed = (promise, key = 'delta') => {
const start = now();
return promise.then(data => ({
...data,
[key]: now() - start
}));
}
const test = () => {
@mcsf
mcsf / generics.js
Last active August 29, 2015 14:25
Generic functions in JS
/** [Context on generic functions, WIP]:
*
* - In JS, our everyday tools are accessible via our types, tucked in their
* prototypes. This is generally adequate, and it provides a clear
* organization of all built-in functions, as well as extended
* functionality. Plus, it allows one to easily find only the methods that
* are relevant to a certain piece of data thanks to object inspection / tab
* completion: myArray.con<TAB> -> contains
*
* - This, however, means that these tools are not first-class functions, but
@mcsf
mcsf / sort-strings-with-integers.js
Last active September 10, 2015 15:35
A compare function that knows that "Thing 5" < "Thing 10"
import { first, filter, zipWith } from 'lodash';
const number = /[0-9]+/;
// compare('Menu 5', 'Menu 10')
// --> -1
//
// compare :: String -> String -> Ordering
function compare(a, b) {
return first(filter(zipWith(split(a), split(b), cmp), Boolean));
import R from 'ramda';
import S from 'sanctuary';
import { equal } from 'assert';
// The gist:
function myModule() {
const myStore = createMonadicReducer({
// Plain (a -> b) functions are wrapped so that they return a Maybe
inc: pure(R.inc),
@mcsf
mcsf / voting-systems.js
Last active October 6, 2015 18:05
Following the 2015 Portuguese legislative elections, a comparison between actual seat allocation and simulations of nationwide (i.e. ignoring electoral districts) application of d'Hondt and Sainte-Laguë methods.
/**
* Following the 2015 Portuguese legislative elections, a comparison between
* actual seat allocation and simulations of nationwide (i.e. ignoring
* electoral districts) application of d'Hondt and Sainte-Laguë methods.
*
* The last three column reflect parliamentary seat assignments.
*
* - "Atribuídos" are the actual seats that were assigned following the
* elections
*
@mcsf
mcsf / unfold-async.js
Created December 16, 2015 15:46
Silly experiments with loops and async
//// 1
// We could use an immediately-invoked function named 'recur' calling itself,
// but that would pollute scope. Thus, use a block and 'const'.
{
const recur = (foo) =>
setTimeout(() => {
console.log('A', Date.now(), foo)
recur('C')
}, 250)

Getting Eslint to work in Vim with Syntastic

The issue: your project, say wp-calypso, assumes a certain version of eslint as specified in package.json. Maybe the project's .eslintrc.js file is incompatible with other versions of Eslint. Meanwhile, you are often expected to run eslint as a global command on your system. That, however, becomes a problem once you have different projects working with different versions of all sorts of Node-based global tools (e.g. eslint, babel, etc.).

The solution is to use npm-which, which returns the path for a certain program that is local to your project. That is:

cd wp-calypso
npm-which eslint # outputs something like $MY_PROJECT_DIR/node_modules/.bin/eslint
# compare with
/**
* Consumes a list by
* - creating a promise from an item
* - waiting until its resolution
* - proceeding to the next item
*
* Returns a promise that waits for the last item's promise to be resolved.
*/
const makePromiseSequence = (list, fn) => list.reduce(
(acc, curr) => acc.then(fn.bind(null, curr)),
@mcsf
mcsf / prop-changes.js
Last active October 13, 2016 14:48
Pretty-formatted diff between old and new props, or context
import deep from 'deep-diff';
import {
defaultTo,
filter,
insert,
join,
map,
pipe,
} from 'ramda';