Skip to content

Instantly share code, notes, and snippets.

View stefanfrede's full-sized avatar

Stefan Frede stefanfrede

View GitHub Profile
@stefanfrede
stefanfrede / unary_decorator_recipe.js
Created June 7, 2016 05:40
Unary takes any function and turns it into a function taking exactly one argument.
const unary = (fn) =>
fn.length === 1
? fn
: function (something) {
return fn.call(this, something);
};
/**
* Example use case
*
@stefanfrede
stefanfrede / tap_combinator_recipe.js
Created June 7, 2016 06:03
The “K Combinator".
/**
* tap takes a value and returns a function that
* always returns the value, but if you pass it a
* function, it executes the function for
* side-effects.
*/
const tap = (value, fn) => {
const curried = (fn) => (
typeof(fn) === 'function' && fn(value),
@stefanfrede
stefanfrede / maybe_decorator_recipe.js
Last active June 9, 2016 04:44
Maybe takes a function and checks for nothing.
/**
* Do nothing when given nothing.
*/
const maybe = (fn) =>
function (...args) {
if (args.length === 0) {
return;
} else {
for (let arg of args) {
if (arg == null) {
@stefanfrede
stefanfrede / once_combinator_recipe.js
Last active June 10, 2016 05:36
Once ensures that a function can only be called once.
/**
* You pass it a function, and you get a function back.
* That function will call your function once, and
* thereafter will return undefined whenever it is
* called.
*/
const once = (fn) => {
let done = false;
return function () {
@stefanfrede
stefanfrede / left_gather_decorator_recipe.js
Created June 10, 2016 05:35
leftGather gathers excess arguments into it from the left.
/**
* Gathering arguments from the right is easy in ES6 with the spread operator
*/
const [first, ...butFirst] = ['why', 'hello', 'there', 'little', 'droid'];
first
//=> 'why'
butFirst
//=> ["hello","there","little","droid"]
@stefanfrede
stefanfrede / left_variadic_decorator_recipe.js
Created June 12, 2016 11:23
leftVariadic is a left-variadic function which has one or more fixed arguments, and gathers the rest into the leftmost argument.
/**
* Gathering arguments from the right is easy in ES6 with the spread operator
*/
const abccc = (a, b, ...c) => {
console.log(a);
console.log(b);
console.log(c);
};
abccc(1, 2, 3, 4, 5)
@stefanfrede
stefanfrede / compose_combinator_recipe.js
Created June 27, 2016 05:14
The B Combinator, or compose.
const compose = (a, b) =>
(c) => a(b(c))
// Given:
const addOne = (number) => number + 1;
const doubleOf = (number) => number * 2;
// Instead of:
@stefanfrede
stefanfrede / variadic_compose_combinator_recipe.js
Created June 27, 2016 05:28
The B Combinator, or (variadic) compose.
const compose = (a, ...rest) =>
rest.length === 0
? a
: (c) => a(compose(...rest)(c))
// Given:
const addOne = (number) => number + 1;
const doubleOf = (number) => number * 2;
@stefanfrede
stefanfrede / pipeline_combinator_recipe.js
Created June 27, 2016 05:32
Compose functions in data flow.
const pipeline = (...fns) =>
(value) =>
fns.reduce((acc, fn) => fn(acc), value);
// Given:
const addOne = (number) => number + 1;
const doubleOf = (number) => number * 2;
@stefanfrede
stefanfrede / mapwith_recipe.js
Created August 3, 2016 05:09
mapWith is a function that wraps around map and turns any other function into a mapper.
const mapWith = (fn) => (list) => list.map(fn);
// Example:
const squaresOf = mapWith(n => n * n);
squaresOf([1, 2, 3, 4, 5])
//=> [1, 4, 9, 16, 25]