From b005826fdec0dbb9f05ff63d9bfecdc895b4a5d0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 28 Jan 2018 14:54:16 +0200 Subject: [PATCH] Add overArgs --- snippets/overArgs.md | 20 ++++++++++++++++++++ tag_database | 1 + 2 files changed, 21 insertions(+) create mode 100644 snippets/overArgs.md diff --git a/snippets/overArgs.md b/snippets/overArgs.md new file mode 100644 index 000000000..0c54fa726 --- /dev/null +++ b/snippets/overArgs.md @@ -0,0 +1,20 @@ +### overArgs + +Creates a function that invokes the provided function with its arguments transformed. + +Use `Array.map()` to apply `transforms` to `args` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`. + +```js +const overArgs = (fn, transforms) => (...args) => + fn(...args.map((val, i) => transforms[i](val))); +``` + +```js +var func = overArgs( + function(x, y) { + return [x, y]; + }, + [square, doubled] +); +func(9, 3); // [81, 6] +``` diff --git a/tag_database b/tag_database index a556e5be3..8a8a891bc 100644 --- a/tag_database +++ b/tag_database @@ -162,6 +162,7 @@ once:function onUserInputChange:browser,event,advanced orderBy:object,array over:adapter,function +overArgs:adapter,function palindrome:string parseCookie:utility,string partial:function