diff --git a/snippets/call.md b/snippets/call.md index e8aea4468..ce176f8fd 100644 --- a/snippets/call.md +++ b/snippets/call.md @@ -1,22 +1,22 @@ ---- -title: call -tags: function,advanced ---- - -Given a key and a set of arguments, call them when given a context. - -- Use a closure to call `key` with `args` for the given `context`. - -```js -const call = (key, ...args) => context => context[key](...args); -``` - -```js -Promise.resolve([1, 2, 3]) - .then(call('map', x => 2 * x)) - .then(console.log); // [ 2, 4, 6 ] -const map = call.bind(null, 'map'); -Promise.resolve([1, 2, 3]) - .then(map(x => 2 * x)) - .then(console.log); // [ 2, 4, 6 ] -``` +--- +title: call +tags: function,advanced +--- + +Given a key and a set of arguments, call them when given a context. + +- Use a closure to call `key` with `args` for the given `context`. + +```js +const call = (key, ...args) => context => context[key](...args); +``` + +```js +Promise.resolve([1, 2, 3]) + .then(call('map', x => 2 * x)) + .then(console.log); // [ 2, 4, 6 ] +const map = call.bind(null, 'map'); +Promise.resolve([1, 2, 3]) + .then(map(x => 2 * x)) + .then(console.log); // [ 2, 4, 6 ] +``` diff --git a/snippets/collectInto.md b/snippets/collectInto.md index 433ec4a2e..c74725e7a 100644 --- a/snippets/collectInto.md +++ b/snippets/collectInto.md @@ -1,20 +1,20 @@ ---- -title: collectInto -tags: function,array,intermediate ---- - -Changes a function that accepts an array into a variadic function. - -- Given a function, return a closure that collects all inputs into an array-accepting function. - -```js -const collectInto = fn => (...args) => fn(args); -``` - -```js -const Pall = collectInto(Promise.all.bind(Promise)); -let p1 = Promise.resolve(1); -let p2 = Promise.resolve(2); -let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3)); -Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds) -``` +--- +title: collectInto +tags: function,array,intermediate +--- + +Changes a function that accepts an array into a variadic function. + +- Given a function, return a closure that collects all inputs into an array-accepting function. + +```js +const collectInto = fn => (...args) => fn(args); +``` + +```js +const Pall = collectInto(Promise.all.bind(Promise)); +let p1 = Promise.resolve(1); +let p2 = Promise.resolve(2); +let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3)); +Pall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds) +``` diff --git a/snippets/flip.md b/snippets/flip.md index ff6adf830..282eb0cd0 100644 --- a/snippets/flip.md +++ b/snippets/flip.md @@ -1,23 +1,23 @@ ---- -title: flip -tags: function,intermediate ---- - -Takes a function as an argument, then makes the first argument the last. - -- Use argument destructuring and a closure with variadic arguments. -- Splice the first argument, using the spread operator (`...`), to make it the last before applying the rest. - -```js -const flip = fn => (first, ...rest) => fn(...rest, first); -``` - -```js -let a = { name: 'John Smith' }; -let b = {}; -const mergeFrom = flip(Object.assign); -let mergePerson = mergeFrom.bind(null, a); -mergePerson(b); // == b -b = {}; -Object.assign(b, a); // == b -``` +--- +title: flip +tags: function,intermediate +--- + +Takes a function as an argument, then makes the first argument the last. + +- Use argument destructuring and a closure with variadic arguments. +- Splice the first argument, using the spread operator (`...`), to make it the last before applying the rest. + +```js +const flip = fn => (first, ...rest) => fn(...rest, first); +``` + +```js +let a = { name: 'John Smith' }; +let b = {}; +const mergeFrom = flip(Object.assign); +let mergePerson = mergeFrom.bind(null, a); +mergePerson(b); // == b +b = {}; +Object.assign(b, a); // == b +``` diff --git a/snippets/spreadOver.md b/snippets/spreadOver.md index b72c7c262..fec5ceb46 100644 --- a/snippets/spreadOver.md +++ b/snippets/spreadOver.md @@ -1,17 +1,17 @@ ---- -title: spreadOver -tags: function,intermediate ---- - -Takes a variadic function and returns a function that accepts an array of arguments. - -- Use a closure and the spread operator (`...`) to map the array of arguments to the inputs of the function. - -```js -const spreadOver = fn => argsArr => fn(...argsArr); -``` - -```js -const arrayMax = spreadOver(Math.max); -arrayMax([1, 2, 3]); // 3 -``` +--- +title: spreadOver +tags: function,intermediate +--- + +Takes a variadic function and returns a function that accepts an array of arguments. + +- Use a closure and the spread operator (`...`) to map the array of arguments to the inputs of the function. + +```js +const spreadOver = fn => argsArr => fn(...argsArr); +``` + +```js +const arrayMax = spreadOver(Math.max); +arrayMax([1, 2, 3]); // 3 +``` diff --git a/snippets/toRGBArray.md b/snippets/toRGBArray.md index de2480816..e55bb0e81 100644 --- a/snippets/toRGBArray.md +++ b/snippets/toRGBArray.md @@ -1,17 +1,17 @@ ---- -title: toRGBArray -tags: string,browser,regexp,beginner ---- - -Converts an `rgb()` color string to an array of values. - -- Use `String.prototype.match()` to get an array of 3 string with the numeric values. -- Use `Array.prototype.map()` in combination with `Number` to convert them into an array of numeric values. - -```js -const toRGBArray = rgbStr => rgbStr.match(/\d+/g).map(Number); -``` - -```js -toRGBArray('rgb(255, 12, 0)'); // [255, 12, 0] -``` +--- +title: toRGBArray +tags: string,browser,regexp,beginner +--- + +Converts an `rgb()` color string to an array of values. + +- Use `String.prototype.match()` to get an array of 3 string with the numeric values. +- Use `Array.prototype.map()` in combination with `Number` to convert them into an array of numeric values. + +```js +const toRGBArray = rgbStr => rgbStr.match(/\d+/g).map(Number); +``` + +```js +toRGBArray('rgb(255, 12, 0)'); // [255, 12, 0] +```