Skip to content

Instantly share code, notes, and snippets.

View davidbarral's full-sized avatar

David Barral davidbarral

View GitHub Profile
@davidbarral
davidbarral / publish
Created May 13, 2019 08:21
Monorepo: new publish script
#!/bin/bash -e
REPO=$@
publishable() {
export REPO
/usr/bin/env node <<'SCRIPT'
const { execSync } = require("child_process");
const local = require("./package.json");
try {
@davidbarral
davidbarral / microservice.jsx
Last active March 26, 2019 09:48
microservices
listen(
<Service>
<Status code={200} />
<Body>ok</Body>
</Service>,
4000
);
const hyphenize = s => s.replace(/([A-Z])/g, g => `-${g[0].toLowerCase()}`);
const classify = (block, element, modifier) => {
const parts = [hyphenize(block)];
if (element) {
parts.push("__");
parts.push(hyphenize(element));
}
// Same as promise.js but with debug messages.
// Just run the code with node promise-debug.js.
// By setting DEBUG to true each promise will output in console some debug info
// There are 13 samples. In terms of debug output is better to focus in one of them at a time.
// You can do that by setting the FOCUS variable (i.e. FOCUS=11)
const DEBUG = false;
const FOCUS = false;
let promiseId = 1;
@davidbarral
davidbarral / ramda-curry.js
Last active February 12, 2018 11:02
Curry: Ramda
import { curry, __ as _ } from "ramda";
const sum4 = curry((a, b, c, d) => a + b + c + d);
// Currying
sum4(1, 2, 3, 4);
sum4()(1, 2, 3, 4);
sum4(1)(2, 3, 4);
sum4(1)(2)(3, 4);
sum4(1)(2)(3)(4);
@davidbarral
davidbarral / curry.js
Last active February 12, 2018 10:40
TDD: curry
expect(sum4(1)(2)(3,4)).toBe(10);
expect(sum4(1)(2)(3)(4)).toBe(10);
expect(sum4(1,2)(3)(4)).toBe(10);
@davidbarral
davidbarral / curry.js
Created February 11, 2018 16:13
Curry: not working version
const curry = fn => (...args) =>
args.length >= fn.length
? fn(...args)
: curry((...innerArgs) => fn(...args, ...innerArgs));
@davidbarral
davidbarral / curry-test.js
Last active February 12, 2018 10:56
Curry: TDD partial application
const expect = x => ({
toBe: y => {
if (x !== y) {
throw new Error(`expected ${x} to be ${y}`);
}
}
});
const sum4 = curry((a, b, c, d) => a + b + c + d);
@davidbarral
davidbarral / curry.js
Created February 11, 2018 16:00
Curry: final version
const curry = fn => {
const curryN = (n, fn) => (...args) =>
args.length >= n
? fn(...args)
: curryN(n - args.length, (...innerArgs) => fn(...args, ...innerArgs));
return curryN(fn.length, fn);
};
@davidbarral
davidbarral / curry.js
Created February 11, 2018 15:57
Curry: partial application
const curry = fn => (...args) =>
args.length >= fn.length
? fn(...args)
: (...innerArgs) => fn(...args, ...innerArgs);