diff --git a/README.md b/README.md index 177725d96..88ee15553 100644 --- a/README.md +++ b/README.md @@ -190,21 +190,13 @@ average(1, 2, 3); * [`defer`](#defer) * [`functionName`](#functionname) * [`memoize`](#memoize) +* [`negate`](#negate) * [`once`](#once) * [`runPromisesInSeries`](#runpromisesinseries) * [`sleep`](#sleep) -### 🔮 Logic - -
-View contents - -* [`negate`](#negate) - -
- ### âž— Math
@@ -2633,6 +2625,28 @@ console.log(anagramsCached.cache); // The cached anagrams map
[⬆ Back to top](#table-of-contents) +### negate + +Negates a predicate function. + +Take a predicate function and apply the not operator (`!`) to it with its arguments. + +```js +const negate = func => (...args) => !func(...args); +``` + +
+Examples + +```js +[1, 2, 3, 4, 5, 6].filter(negate(n => n % 2 == 0)); // [ 1, 3, 5 ] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### once Ensures a function is called only once. @@ -2714,31 +2728,6 @@ async function sleepyWork() {
[⬆ Back to top](#table-of-contents) ---- - ## 🔮 Logic - -### negate - -Negates a predicate function. - -Take a predicate function and apply the not operator (`!`) to it with its arguments. - -```js -const negate = func => (...args) => !func(...args); -``` - -
-Examples - -```js -filter([1, 2, 3, 4, 5, 6], negate(isEven)); // [1, 3, 5] -negate(isOdd)(1); // false -``` - -
- -
[⬆ Back to top](#table-of-contents) - --- ## ➗ Math diff --git a/docs/index.html b/docs/index.html index 352b4e736..269bb47a2 100644 --- a/docs/index.html +++ b/docs/index.html @@ -40,7 +40,7 @@ },1700); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -544,6 +544,8 @@ document.que
 anagramsCached('javascript'); // takes a long time
 anagramsCached('javascript'); // returns virtually instantly since it's now cached
 console.log(anagramsCached.cache); // The cached anagrams map
+

negate

Negates a predicate function.

Take a predicate function and apply the not operator (!) to it with its arguments.

const negate = func => (...args) => !func(...args);
+
[1, 2, 3, 4, 5, 6].filter(negate(n => n % 2 == 0)); // [ 1, 3, 5 ]
 

once

Ensures a function is called only once.

Utilizing a closure, use a flag, called, and set it to true once the function is called for the first time, preventing it from being called again. In order to allow the function to have its this context changed (such as in an event listener), the function keyword must be used, and the supplied function must have the context applied. Allow the function to be supplied with an arbitrary number of arguments using the rest/spread (...) operator.

const once = fn => {
   let called = false;
   return function(...args) {
@@ -565,9 +567,6 @@ document.bodyawait sleep(1000);
   console.log('I woke up after 1 second.');
 }
-

Logic

negate

Negates a predicate function.

Take a predicate function and apply the not operator (!) to it with its arguments.

const negate = func => (...args) => !func(...args);
-
filter([1, 2, 3, 4, 5, 6], negate(isEven)); // [1, 3, 5]
-negate(isOdd)(1); // false
 

Math

average

Returns the average of an of two or more numbers.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;
 
average(...[1, 2, 3]); // 2
 average(1, 2, 3); // 2