Travis build: 960

This commit is contained in:
30secondsofcode
2018-01-03 15:04:56 +00:00
parent 496da3e64a
commit 10b0c1d618
3 changed files with 55 additions and 154 deletions

186
README.md
View File

@ -229,9 +229,7 @@ average(1, 2, 3);
* [`isEven`](#iseven)
* [`isPrime`](#isprime)
* [`lcm`](#lcm)
* [`maxN`](#maxn)
* [`median`](#median)
* [`minN`](#minn)
* [`percentile`](#percentile)
* [`powerset`](#powerset)
* [`primes`](#primes)
@ -342,29 +340,19 @@ average(1, 2, 3);
</details>
### _Uncategorized_
<details>
<summary>View contents</summary>
* [`maxN`](#maxn)
* [`minN`](#minn)
</details>
---
## 🔌 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.
### 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.
```js
const call = (key, ...args) => context => context[key](...args);
```
```
<details>
<summary>Examples</summary>
@ -376,23 +364,23 @@ const map = call.bind(null, 'map');
Promise.resolve([1, 2, 3])
.then(map(x => 2 * x))
.then(console.log); //[ 2, 4, 6 ]
```
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### collectInto
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.
### collectInto
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);
```
```
<details>
<summary>Examples</summary>
@ -402,23 +390,23 @@ 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);
```
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### flip
Flip takes a function as an argument, then makes the first argument the last
Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
### flip
Flip takes a function as an argument, then makes the first argument the last
Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
```js
const flip = fn => (...args) => fn(args.pop(), ...args);
```
```
<details>
<summary>Examples</summary>
@ -430,7 +418,7 @@ let mergePerson = mergeFrom.bind(null, a);
mergePerson(b); // == b
b = {};
Object.assign(b, a); // == b
```
```
</details>
@ -492,16 +480,16 @@ delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s
<br>[⬆ Back to top](#table-of-contents)
### spreadOver
Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.
Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function.
### spreadOver
Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.
Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function.
```js
const spreadOver = fn => argsArr => fn(...argsArr);
```
```
<details>
<summary>Examples</summary>
@ -509,7 +497,7 @@ const spreadOver = fn => argsArr => fn(...argsArr);
const arrayMax = spreadOver(Math.max);
arrayMax([1, 2, 3]); // 3
arrayMax([1, 2, 4]); // 4
```
```
</details>
@ -2556,13 +2544,18 @@ functionName(Math.max); // max (logged in debug channel of console)
Returns the memoized (cached) function.
Use `Object.create(null)` to create an empty object without `Object.prototype` (so that those properties are not resolved if the input value is something like `'hasOwnProperty'`).
Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not.
Create an empty cache by instantiating a new `Map` object.
Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. The `function` keyword must be used in order to allow the memoized function to have its `this` context changed if necessary.
Allow access to the `cache` by setting it as a property on the returned function.
```js
const memoize = fn => {
const cache = Object.create(null);
return value => cache[value] || (cache[value] = fn(value));
const cache = new Map();
const cached = function(val) {
return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val);
};
cached.cache = cache;
return cached;
};
```
@ -2574,6 +2567,7 @@ const memoize = fn => {
const anagramsCached = memoize(anagrams);
anagramsCached('javascript'); // takes a long time
anagramsCached('javascript'); // returns virtually instantly since it's now cached
console.log(anagramsCached.cache); // Map
```
</details>
@ -3283,31 +3277,6 @@ lcm([1, 3, 4], 5); // 60
<br>[⬆ Back to top](#table-of-contents)
### maxN
Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order).
Sort's the array's shallow copy in descending order and returns the first n elements
Skip the second argument to get a single element(in the form of a array)
```js
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
```
<details>
<summary>Examples</summary>
```js
maxN([1, 2, 3]); // [3]
maxN([1, 2, 3], 2); // [3,2]
maxN([1, 2, 3], 4); // [3,2,1]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### median
Returns the median of an array of numbers.
@ -3336,30 +3305,6 @@ median([0, 10, -2, 7]); // 3.5
<br>[⬆ Back to top](#table-of-contents)
### minN
Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
Sort's the array's shallow copy in ascending order and returns the first n elements
Skip the second argument to get a single element(in the form of a array)
```js
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
```
<details>
<summary>Examples</summary>
```js
minN([1, 2, 3]); // [1]
minN([1, 2, 3], 2); // [1,2]
minN([1, 2, 3], 4); // [1,2,3]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### percentile
Uses the percentile formula to calculate how many numbers in the given array are less or equal to the given value.
@ -5410,45 +5355,6 @@ yesNo('Foo', true); // true
<br>[⬆ Back to top](#table-of-contents)
### maxN
Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order).
Sort's the array's shallow copy in descending order and returns the first n elements
Skip the second argument to get a single element(in the form of a array)
```js
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
```
```js
maxN([1, 2, 3]); // [3]
maxN([1, 2, 3], 2); // [3,2]
maxN([1, 2, 3], 4); // [3,2,1]
```
<br>[⬆ back to top](#table-of-contents)
### minN
Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
Sort's the array's shallow copy in ascending order and returns the first n elements
Skip the second argument to get a single element(in the form of a array)
```js
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
```
```js
minN([1, 2, 3]); // [1]
minN([1, 2, 3], 2); // [1,2]
minN([1, 2, 3], 4); // [1,2,3]
```
<br>[⬆ back to top](#table-of-contents)
## Collaborators
| [<img src="https://github.com/Chalarangelo.png" width="100px;"/>](https://github.com/Chalarangelo)<br/> [<sub>Angelos Chalaris</sub>](https://github.com/Chalarangelo) | [<img src="https://github.com/Pl4gue.png" width="100px;"/>](https://github.com/Pl4gue)<br/> [<sub>David Wu</sub>](https://github.com/Pl4gue) | [<img src="https://github.com/fejes713.png" width="100px;"/>](https://github.com/fejes713)<br/> [<sub>Stefan Feješ</sub>](https://github.com/fejes713) | [<img src="https://github.com/kingdavidmartins.png" width="100px;"/>](https://github.com/kingdavidmartins)<br/> [<sub>King David Martins</sub>](https://github.com/iamsoorena) | [<img src="https://github.com/iamsoorena.png" width="100px;"/>](https://github.com/iamsoorena)<br/> [<sub>Soorena Soleimani</sub>](https://github.com/iamsoorena) |

View File

@ -546,14 +546,19 @@ longRunningFunction(); // the browser will not update the HTML until this has fi
defer(longRunningFunction); // the browser will update the HTML then run the function
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="functionname">functionName</h3></div><div class="section double-padded"><p>Logs the name of a function.</p><p>Use <code>console.debug()</code> and the <code>name</code> property of the passed method to log the method's name to the <code>debug</code> channel of the console.</p><pre><code class="language-js">const functionName = fn =&gt; (console.debug(fn.name), fn);
</code></pre><pre><code class="language-js">functionName(Math.max); // max (logged in debug channel of console)
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="memoize">memoize</h3></div><div class="section double-padded"><p>Returns the memoized (cached) function.</p><p>Use <code>Object.create(null)</code> to create an empty object without <code>Object.prototype</code> (so that those properties are not resolved if the input value is something like <code>'hasOwnProperty'</code>). Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not.</p><pre><code class="language-js">const memoize = fn =&gt; {
const cache = Object.create(null);
return value =&gt; cache[value] || (cache[value] = fn(value));
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="memoize">memoize</h3></div><div class="section double-padded"><p>Returns the memoized (cached) function.</p><p>Create an empty cache by instantiating a new <code>Map</code> object. Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. The <code>function</code> keyword must be used in order to allow the memoized function to have its <code>this</code> context changed if necessary. Allow access to the <code>cache</code> by setting it as a property on the returned function.</p><pre><code class="language-js">const memoize = fn =&gt; {
const cache = new Map();
const cached = function(val) {
return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) &amp;&amp; cache.get(val);
};
cached.cache = cache;
return cached;
};
</code></pre><pre><code class="language-js">// See the `anagrams` snippet.
const anagramsCached = memoize(anagrams);
anagramsCached('javascript'); // takes a long time
anagramsCached('javascript'); // returns virtually instantly since it's now cached
console.log(anagramsCached.cache); // Map
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="once">once</h3></div><div class="section double-padded"><p>Ensures a function is called only once.</p><p>Utilizing a closure, use a flag, <code>called</code>, and set it to <code>true</code> once the function is called for the first time, preventing it from being called again. In order to allow the function to have its <code>this</code> context changed (such as in an event listener), the <code>function</code> 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 (<code>...</code>) operator.</p><pre><code class="language-js">const once = fn =&gt; {
let called = false;
return function(...args) {
@ -717,10 +722,6 @@ isPrime(12); // false
};
</code></pre><pre><code class="language-js">lcm(12, 7); // 84
lcm([1, 3, 4], 5); // 60
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="maxn">maxN</h3></div><div class="section double-padded"><p>Returns the <code>n</code> maximum elements from the provided array. If <code>n</code> is greater than or equal to the provided array's length than return the original array(sorted in descending order).</p><p>Sort's the array's shallow copy in descending order and returns the first n elements</p><p>Skip the second argument to get a single element(in the form of a array)</p><pre><code class="language-js">const maxN = (arr, n = 1) =&gt; [...arr].sort((a, b) =&gt; b - a).slice(0, n);
</code></pre><pre><code class="language-js">maxN([1, 2, 3]); // [3]
maxN([1, 2, 3], 2); // [3,2]
maxN([1, 2, 3], 4); // [3,2,1]
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="median">median</h3></div><div class="section double-padded"><p>Returns the median of an array of numbers.</p><p>Find the middle of the array, use <code>Array.sort()</code> to sort the values. Return the number at the midpoint if <code>length</code> is odd, otherwise the average of the two middle numbers.</p><pre><code class="language-js">const median = arr =&gt; {
const mid = Math.floor(arr.length / 2),
nums = [...arr].sort((a, b) =&gt; a - b);
@ -728,10 +729,6 @@ maxN([1, 2, 3], 4); // [3,2,1]
};
</code></pre><pre><code class="language-js">median([5, 6, 50, 1, -5]); // 5
median([0, 10, -2, 7]); // 3.5
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="minn">minN</h3></div><div class="section double-padded"><p>Returns the <code>n</code> minimum elements from the provided array. If <code>n</code> is greater than or equal to the provided array's length than return the original array(sorted in ascending order).</p><p>Sort's the array's shallow copy in ascending order and returns the first n elements</p><p>Skip the second argument to get a single element(in the form of a array)</p><pre><code class="language-js">const minN = (arr, n = 1) =&gt; [...arr].sort((a, b) =&gt; a - b).slice(0, n);
</code></pre><pre><code class="language-js">minN([1, 2, 3]); // [1]
minN([1, 2, 3], 2); // [1,2]
minN([1, 2, 3], 4); // [1,2,3]
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="percentile">percentile</h3></div><div class="section double-padded"><p>Uses the percentile formula to calculate how many numbers in the given array are less or equal to the given value.</p><p>Use <code>Array.reduce()</code> to calculate how many numbers are below the value and how many are the same value and apply the percentile formula.</p><pre><code class="language-js">const percentile = (arr, val) =&gt;
100 * arr.reduce((acc, v) =&gt; acc + (v &lt; val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
</code></pre><pre><code class="language-js">percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 6); // 55

View File

@ -10,9 +10,7 @@ Allow access to the `cache` by setting it as a property on the returned function
const memoize = fn => {
const cache = new Map();
const cached = function(val) {
return cache.has(val)
? cache.get(val)
: cache.set(val, fn.call(this, val)) && cache.get(val);
return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val);
};
cached.cache = cache;
return cached;