Created
September 30, 2017 03:57
-
-
Save siriniok/a0fab9e6b6c1398da3a01a61b6134995 to your computer and use it in GitHub Desktop.
Notes from Allonge Six
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 _ = require('lodash'); | |
const map = _.map; | |
const mapWith = (fn) => (list) => map(list, fn); | |
// Bounding to a name | |
((diameter_fn) => | |
console.log(diameter_fn(2)) //=> 6.2831853 | |
)( | |
((PI) => | |
(diameter) => diameter * PI | |
)(3.14159265) | |
); | |
// Combinators | |
const identity = (x) => x; // I Combinator | |
const kestrel = (x) => (y) => x; // K Combinator | |
const composeComb = (a, b) => (c) => a(b(c)); // B Combinator | |
// Decorators | |
const not = (fn) => (x) => !fn(x); // decorator | |
const something = (x) => x != null; | |
// const nothing = (x) => !something(x); | |
const nothing = not(something); | |
// Partial Appliacation | |
const callLeft = (fn, ...args) => | |
(...remainingArgs) => | |
fn(...args, ...remainingArgs); | |
const callRight = (fn, ...args) => | |
(...remainingArgs) => | |
fn(...remainingArgs, ...args); | |
const squareAll = callRight(map, (n) => n * n); | |
console.log(squareAll([1, 2, 3])); //=> [1, 4, 9] | |
// Turns function into a function taking exactly one argument. | |
const unary = (fn) => | |
fn.length === 1 | |
? fn | |
: function (something) { | |
return fn.call(this, something); | |
}; | |
['1', '2', '3'].map(unary(parseInt)); //=> [1, 2, 3] | |
// Does something with value for side-effects, but keeps the value around | |
const tap = (value, fn) => { | |
const curried = (fn) => ( | |
typeof(fn) === 'function' && fn(value), | |
value | |
); | |
return fn === undefined | |
? curried | |
: curried(fn); | |
}; | |
tap([1, 2, 3])((a) => map(a, (e) => e * 4)); //=> [1, 2, 3] | |
// or | |
tap([1, 2, 3], (a) => map(a, (e) => e * 4)); // same | |
// Returns nothing if any of function arguments is null | |
const maybe = (fn) => | |
function (...args) { | |
if (args.length === 0) return; | |
for(let arg of args) { | |
if(arg == null) return; | |
} | |
return fn.apply(this, args); | |
}; | |
maybe((a, b, c) => a + b + c)(1, 2, 3); //=> 6 | |
maybe((a, b, c) => a + b + c)(1, null, 3); //=> undefined | |
// Ensures that a function can only be called once | |
const once = (fn) => { | |
let done = false; | |
return function () { | |
return done ? void 0 : ((done = true), fn.apply(this, arguments)); | |
}; | |
} | |
const askedOnBlindDate = once( | |
() => "sure, why not?" | |
); | |
askedOnBlindDate() | |
//=> 'sure, why not?' | |
askedOnBlindDate() | |
//=> undefined | |
askedOnBlindDate() | |
//=> undefined | |
// Variadic Compose | |
const compose = (a, ...rest) => | |
rest.length === 0 | |
? a | |
: (c) => a(compose(...rest)(c)); | |
// Flatten the given array | |
const flatten = ([first, ...rest]) => { | |
if (first === undefined) return []; | |
if (!Array.isArray(first)) return [first, ...flatten(rest)]; | |
return [...flatten(first), ...flatten(rest)]; | |
} | |
flatten(["foo", [3, 4, []]]) | |
//=> ["foo",3,4] | |
// Just reduce | |
const foldWith = (fn, terminalValue, [first, ...rest]) => | |
first === undefined | |
? terminalValue | |
: fn(first, foldWith(fn, terminalValue, rest)); | |
foldWith((number, rest) => number * number + rest, 0, [1, 2, 3, 4, 5]) | |
//=> 55 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment