Skip to content

Instantly share code, notes, and snippets.

View stefanfrede's full-sized avatar

Stefan Frede stefanfrede

View GitHub Profile
@stefanfrede
stefanfrede / deepmapping_recipe.js
Created August 23, 2016 06:08
Working with arrays that represent trees.
const deepMapWith = (fn) =>
function innerdeepMapWith (tree) {
return Array.prototype.map.call(tree, (element) =>
Array.isArray(element)
? innerdeepMapWith(element)
: fn(element)
);
}
/**
@stefanfrede
stefanfrede / pluckwith_recipe.js
Created August 22, 2016 05:23
Combine the mapWith and the getWith recipe.
const pluckWith = (attr) => mapWith(getWith(attr));
// or even better
const pluckWith = compose(mapWith, getWith);
/**
* Example
*/
const inventories = [
@stefanfrede
stefanfrede / getwith_recipe.js
Created August 20, 2016 06:03
getWith takes the name of an attribute and returns a function that extracts the value of that attribute from an object.
const getWith = (attr) => (object) => object[attr]
/**
* Examples:
*/
const inventory = {
apples: 0,
oranges: 144,
eggs: 36
};
@stefanfrede
stefanfrede / memoized_recipe.js
Created August 19, 2016 06:28
Use a lookup table for recursive functions to look up results to avoid recalculation
const memoized = (fn) => {
const lookupTable = {};
return function (...args) {
const key = JSON.stringify(this, args);
return lookupTable[key] || (lookupTable[key] = fn.apply(this, args));
}
}
@stefanfrede
stefanfrede / object_assign_recipe.js
Created August 3, 2016 05:28
Extend objects by assigning properties.
// Copy an object by extending an empty object:
Object.assign({}, {
apples: 12,
oranges: 12
})
//=> { apples: 12, oranges: 12 }
// Extend one object with another:
const inventory = {
apples: 12,
@stefanfrede
stefanfrede / flipandcurry_recipe.js
Created August 3, 2016 05:22
flipAndCurry is a function that takes a function as argument, “flips” the order of arguments around, and then curries it.
const flipAndCurry = (fn) =>
(first) => (second) => fn(second, first);
// Example:
// https://gist.github.com/stefanfrede/596597d8b544de08491364eab20053c6
const mapWith = flipAndCurry(map);
// flipAndCurry throws the current context away, so it can’t be used to flip methods.
// A small alteration gets the job done:
const flipAndCurry = (fn) =>
@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]
@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 / 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 / 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: