diff --git a/snippets/array-zip.md b/snippets/array-zip.md index 6c9a2491b..5c3193605 100644 --- a/snippets/array-zip.md +++ b/snippets/array-zip.md @@ -6,7 +6,7 @@ If lengths of the argument-arrays vary, `undefined` is used where no value could ```js const zip = (...arrays) => { - const maxLength = Math.max.apply(null, arrays.map(a => a.length)); + const maxLength = Math.max(...arrays.map(x => x.length)); return Array.from({length: maxLength}).map((_, i) => { return Array.from({length: arrays.length}, (_, k) => arrays[k][i]); }) diff --git a/snippets/compose-functions.md b/snippets/compose-functions.md index cd60bb7e0..d61c9f3b6 100644 --- a/snippets/compose-functions.md +++ b/snippets/compose-functions.md @@ -1,15 +1,14 @@ ### Compose functions -Use the `...rest` operator to gather all function arguments into an array. Return a function which takes -a single argument and uses `Array.reduceRight()` to return the result of applying each function. +Use `Array.reduce()` to perform right-to-left function composition. +The last (rightmost) function can accept one or more arguments; the remaining functions must be unary. ```js -const compose = (...fns) => n => fns.reduceRight((acc, fn) => fn(acc), n); +const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); /* -const addOne = n => n + 1; -const square = n => n * n; -const double = n => n * 2; -compose(addOne, square, double)(2) -> 17 -equivalent to: addOne(square(double(2))) +const add5 = x => x + 5 +const multiply = (x, y) => x * y +const multiplyAndAdd5 = compose(add5, multiply) +multiplyAndAdd5(5, 2) -> 15 */ -``` +``` \ No newline at end of file diff --git a/snippets/pipe.md b/snippets/pipe.md index e5136777b..434b4c0c3 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -1,6 +1,6 @@ ### Pipe -Use `Array.reduce()` to perform left-to-right function composition. +Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary. ```js