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 unary = (fn) => | |
fn.length === 1 | |
? fn | |
: function (something) { | |
return fn.call(this, something); | |
}; | |
/** | |
* Example use case | |
* |
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
/** | |
* 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), |
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
/** | |
* Do nothing when given nothing. | |
*/ | |
const maybe = (fn) => | |
function (...args) { | |
if (args.length === 0) { | |
return; | |
} else { | |
for (let arg of args) { | |
if (arg == null) { |
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
/** | |
* 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 () { |
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
/** | |
* 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"] |
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
/** | |
* 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) |
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 compose = (a, b) => | |
(c) => a(b(c)) | |
// Given: | |
const addOne = (number) => number + 1; | |
const doubleOf = (number) => number * 2; | |
// Instead of: |
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 compose = (a, ...rest) => | |
rest.length === 0 | |
? a | |
: (c) => a(compose(...rest)(c)) | |
// Given: | |
const addOne = (number) => number + 1; | |
const doubleOf = (number) => number * 2; |
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 pipeline = (...fns) => | |
(value) => | |
fns.reduce((acc, fn) => fn(acc), value); | |
// Given: | |
const addOne = (number) => number + 1; | |
const doubleOf = (number) => number * 2; |
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 mapWith = (fn) => (list) => list.map(fn); | |
// Example: | |
const squaresOf = mapWith(n => n * n); | |
squaresOf([1, 2, 3, 4, 5]) | |
//=> [1, 4, 9, 16, 25] |