From bc58a5bf54b6e227a2bfef996024d6dfb63363f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 25 Dec 2017 14:33:49 +0100 Subject: [PATCH] update snippets 78 - 96 --- snippets/JSONToFile.md | 5 ++++- snippets/last.md | 5 ++++- snippets/lcm.md | 5 ++++- snippets/mapObject.md | 7 ++++--- snippets/median.md | 7 +++++-- snippets/negate.md | 7 +++++-- snippets/nthElement.md | 7 +++++-- snippets/objectFromPairs.md | 5 ++++- snippets/objectToPairs.md | 5 ++++- snippets/orderBy.md | 5 +++-- snippets/palindrome.md | 7 +++++-- snippets/percentile.md | 7 +++++-- snippets/pick.md | 5 ++++- snippets/pipe.md | 5 +++-- snippets/powerset.md | 5 ++++- snippets/primes.md | 11 +++++++---- snippets/promisify.md | 7 +++++-- snippets/pull.md | 20 +++++++++++--------- 18 files changed, 86 insertions(+), 39 deletions(-) diff --git a/snippets/JSONToFile.md b/snippets/JSONToFile.md index 3677fc5e3..300b47e1f 100644 --- a/snippets/JSONToFile.md +++ b/snippets/JSONToFile.md @@ -7,5 +7,8 @@ Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` ```js const fs = require('fs'); const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)) -// JSONToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json' +``` + +```js +JSONToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json' ``` diff --git a/snippets/last.md b/snippets/last.md index 481e2d6e0..f26427591 100644 --- a/snippets/last.md +++ b/snippets/last.md @@ -6,5 +6,8 @@ Use `arr.length - 1` to compute the index of the last element of the given array ```js const last = arr => arr[arr.length - 1]; -// last([1,2,3]) -> 3 +``` + +```js +last([1,2,3]) -> 3 ``` diff --git a/snippets/lcm.md b/snippets/lcm.md index 73b9c43c8..cc9b32367 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -10,5 +10,8 @@ const lcm = (x,y) => { const gcd = (x, y) => !y ? x : gcd(y, x % y); return Math.abs(x*y)/(gcd(x,y)); }; -// lcm(12,7) -> 84 +``` + +```js +lcm(12,7) -> 84 ``` diff --git a/snippets/mapObject.md b/snippets/mapObject.md index eb5ea736f..3eebc515f 100644 --- a/snippets/mapObject.md +++ b/snippets/mapObject.md @@ -5,10 +5,11 @@ Maps the values of an array to an object using a function, where the key-value p Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new `Array` to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations). ```js -const mapObject = (arr, fn) => +const mapObject = (arr, fn) => (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ); -/* +``` + +```js const squareIt = arr => mapObject(arr, a => a*a) squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 } -*/ ``` diff --git a/snippets/median.md b/snippets/median.md index c7eea609c..e472e7c0d 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -10,6 +10,9 @@ const median = arr => { const mid = Math.floor(arr.length / 2), nums = [...arr].sort((a, b) => a - b); return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; }; -// median([5,6,50,1,-5]) -> 5 -// median([0,10,-2,7]) -> 3.5 +``` + +```js +median([5,6,50,1,-5]) -> 5 +median([0,10,-2,7]) -> 3.5 ``` diff --git a/snippets/negate.md b/snippets/negate.md index 5f14e4a0f..739ad97c1 100644 --- a/snippets/negate.md +++ b/snippets/negate.md @@ -6,6 +6,9 @@ Take a predicate function and apply `not` to it with its arguments. ```js const negate = func => (...args) => !func(...args); -// filter([1, 2, 3, 4, 5, 6], negate(isEven)) -> [1, 3, 5] -// negate(isOdd)(1) -> false +``` + +```js +filter([1, 2, 3, 4, 5, 6], negate(isEven)) -> [1, 3, 5] +negate(isOdd)(1) -> false ``` diff --git a/snippets/nthElement.md b/snippets/nthElement.md index ae472eb38..ba2d1933e 100644 --- a/snippets/nthElement.md +++ b/snippets/nthElement.md @@ -8,6 +8,9 @@ Omit the second argument, `n`, to get the first element of the array. ```js const nthElement = (arr, n=0) => (n>0? arr.slice(n,n+1) : arr.slice(n))[0]; -// nthElement(['a','b','c'],1) -> 'b' -// nthElement(['a','b','b'],-3) -> 'a' +``` + +```js +nthElement(['a','b','c'],1) -> 'b' +nthElement(['a','b','b'],-3) -> 'a' ``` diff --git a/snippets/objectFromPairs.md b/snippets/objectFromPairs.md index ebdacb92c..2ed208f44 100644 --- a/snippets/objectFromPairs.md +++ b/snippets/objectFromPairs.md @@ -6,5 +6,8 @@ Use `Array.reduce()` to create and combine key-value pairs. ```js const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {}); -// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} +``` + +```js +objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} ``` diff --git a/snippets/objectToPairs.md b/snippets/objectToPairs.md index 7a6079650..6ea5fc051 100644 --- a/snippets/objectToPairs.md +++ b/snippets/objectToPairs.md @@ -6,5 +6,8 @@ Use `Object.keys()` and `Array.map()` to iterate over the object's keys and prod ```js const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); -// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]]) +``` + +```js +objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]]) ``` diff --git a/snippets/orderBy.md b/snippets/orderBy.md index c80b3667f..ab5581dc3 100644 --- a/snippets/orderBy.md +++ b/snippets/orderBy.md @@ -16,10 +16,11 @@ const orderBy = (arr, props, orders) => return acc; }, 0) ); -/* +``` + +```js const users = [{ 'name': 'fred', 'age': 48 },{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 },{ 'name': 'barney', 'age': 34 }]; orderby(users, ['name', 'age'], ['asc', 'desc']) -> [{name: 'barney', age: 36}, {name: 'barney', age: 34}, {name: 'fred', age: 48}, {name: 'fred', age: 40}] orderby(users, ['name', 'age']) -> [{name: 'barney', age: 34}, {name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}] -*/ ``` diff --git a/snippets/palindrome.md b/snippets/palindrome.md index cca1bb729..820162617 100644 --- a/snippets/palindrome.md +++ b/snippets/palindrome.md @@ -10,5 +10,8 @@ const palindrome = str => { const s = str.toLowerCase().replace(/[\W_]/g,''); return s === s.split('').reverse().join(''); } -// palindrome('taco cat') -> true - ``` +``` + +```js +palindrome('taco cat') -> true +``` diff --git a/snippets/percentile.md b/snippets/percentile.md index 7485494e2..5287521b0 100644 --- a/snippets/percentile.md +++ b/snippets/percentile.md @@ -7,5 +7,8 @@ Use `Array.reduce()` to calculate how many numbers are below the value and how m ```js const percentile = (arr, val) => 100 * arr.reduce((acc,v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length; -// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55 - ``` +``` + +```js +percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55 +``` diff --git a/snippets/pick.md b/snippets/pick.md index 668c9d30c..0bfeba286 100644 --- a/snippets/pick.md +++ b/snippets/pick.md @@ -7,5 +7,8 @@ Use `Array.reduce()` to convert the filtered/picked keys back to an object with ```js const pick = (obj, arr) => arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {}); -// pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 } +``` + +```js +pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 } ``` diff --git a/snippets/pipe.md b/snippets/pipe.md index 4c6495cda..a7c271afe 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -7,10 +7,11 @@ The first (leftmost) function can accept one or more arguments; the remaining fu ```js const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))); -/* +``` + +```js const add5 = x => x + 5 const multiply = (x, y) => x * y const multiplyAndAdd5 = pipeFunctions(multiply, add5) multiplyAndAdd5(5, 2) -> 15 -*/ ``` diff --git a/snippets/powerset.md b/snippets/powerset.md index 09b233212..dc91406e9 100644 --- a/snippets/powerset.md +++ b/snippets/powerset.md @@ -7,5 +7,8 @@ Use `Array.reduce()` combined with `Array.map()` to iterate over elements and co ```js const powerset = arr => arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]); -// powerset([1,2]) -> [[], [1], [2], [2,1]] +``` + +```js +powerset([1,2]) -> [[], [1], [2], [2,1]] ``` diff --git a/snippets/primes.md b/snippets/primes.md index 81e8f2999..c8b64f1e5 100644 --- a/snippets/primes.md +++ b/snippets/primes.md @@ -1,4 +1,4 @@ -### primes +### primes Generates primes up to a given number, using the Sieve of Eratosthenes. @@ -6,11 +6,14 @@ Generate an array from `2` to the given number. Use `Array.filter()` to filter o ```js const primes = num => { - let arr = Array.from({length:num-1}).map((x,i)=> i+2), + let arr = Array.from({length:num-1}).map((x,i)=> i+2), sqroot = Math.floor(Math.sqrt(num)), numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2); numsTillSqroot.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y==x))); - return arr; + return arr; } -// primes(10) -> [2,3,5,7] +``` + +```js +primes(10) -> [2,3,5,7] ``` diff --git a/snippets/promisify.md b/snippets/promisify.md index 80a7d48d0..4769fa5e9 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -14,6 +14,9 @@ const promisify = func => func(...args, (err, result) => err ? reject(err) : resolve(result)) ); -// const delay = promisify((d, cb) => setTimeout(cb, d)) -// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s +``` + +```js +const delay = promisify((d, cb) => setTimeout(cb, d)) +delay(2000).then(() => console.log('Hi!')) -> // Promise resolves after 2s ``` diff --git a/snippets/pull.md b/snippets/pull.md index d7d0412c0..52e0f3ea6 100644 --- a/snippets/pull.md +++ b/snippets/pull.md @@ -11,15 +11,17 @@ _(For a snippet that does not mutate the original array see [`without`](#without const pull = (arr, ...args) => { let argState = Array.isArray(args[0]) ? args[0] : args; let pulled = arr.filter((v, i) => !argState.includes(v)); - arr.length = 0; + arr.length = 0; pulled.forEach(v => arr.push(v)); }; - -// let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c']; -// pull(myArray1, 'a', 'c'); -// console.log(myArray1) -> [ 'b', 'b' ] - -// let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c']; -// pull(myArray2, ['a', 'c']); -// console.log(myArray2) -> [ 'b', 'b' ] +``` + +```js +let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c']; +pull(myArray1, 'a', 'c'); +console.log(myArray1) -> [ 'b', 'b' ] + +let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c']; +pull(myArray2, ['a', 'c']); +console.log(myArray2) -> [ 'b', 'b' ] ```