diff --git a/snippets/rearg.md b/snippets/rearg.md index f58ea2f58..15b7f689c 100644 --- a/snippets/rearg.md +++ b/snippets/rearg.md @@ -2,16 +2,10 @@ Creates a function that invokes the provided function with its arguments arranged according to the specified indexes. -Use `Array.reduce()` and `Array.indexOf()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`. +Use `Array.map()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`. ```js -const rearg = (fn, indexes) => (...args) => - fn( - ...args.reduce( - (acc, val, i) => ((acc[indexes.indexOf(i)] = val), acc), - Array.from({ length: indexes.length }) - ) - ); +const rearg = (fn, indexes) => (...args) => fn(...indexes.map(idx => args[idx])); ``` ```js diff --git a/test/rearg/rearg.js b/test/rearg/rearg.js index 80281fc2e..310261bf3 100644 --- a/test/rearg/rearg.js +++ b/test/rearg/rearg.js @@ -1,8 +1,2 @@ -const rearg = (fn, indexes) => (...args) => - fn( - ...args.reduce( - (acc, val, i) => ((acc[indexes.indexOf(i)] = val), acc), - Array.from({ length: indexes.length }) - ) - ); +const rearg = (fn, indexes) => (...args) => fn(...indexes.map(idx => args[idx])); module.exports = rearg;