This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const animals = [ | |
{animal: 'zebra', count: 4}, {animal: 'tiger', count: 2}, | |
{animal: 'lion', count: 3}, {animal: 'turtle', count: 9}, | |
{animal: 'pig', count: 2}, | |
]; | |
console.log('reduce() example'); | |
const count = animals.reduce((total, curr) => { | |
return total += curr.count; | |
}, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ]; | |
console.log('map() example'); | |
const upperCaseFruits = fruits.map((value) => { | |
return value.toUpperCase(); | |
}); | |
console.log('upperCaseFruits =', upperCaseFruits, '\n'); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ]; | |
const matchedFruits = fruits.filter((element) => { | |
return element.includes('apple'); | |
}); | |
let matchedFruitsAgain = []; | |
for (let i = 0; i < fruits.length; i++) { | |
if (fruits[i].includes('apple')) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fruits = ['pineapple', 'apple', 'banana','pear']; | |
const aFruit = fruits.find((element) => { | |
return element.includes('apple'); | |
}); | |
let aFruit; | |
for (let i = 0; i < fruits.length; i++) { | |
if (fruits[i].includes('apple')) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ]; | |
// improved looping | |
fruits.forEach((value, index, array) => { | |
console.log('fruit at index', index, '=', value); | |
}); | |
// what you may already be used to | |
for (let i=0; i<fruits.length; i++) { | |
console.log('fruit at index', i, '=', fruits[i]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from github import Github | |
g = Github('token') | |
me = g.get_user() | |
repos = me.get_repos() | |
def is_a_repo_contributor(repo): | |
contributors = repo.get_contributors() | |
return me.id in map(lambda con: con.id, contributors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// before - 5 args too many | |
function initializeNewUserWorflow( | |
email, password, location, | |
organization, referred) { | |
// initialize the new user | |
} | |
// after - using configuration object | |
const initializeUserConfiguration = { | |
email: 'email', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// before method simplification | |
function parseAndSanitizeAndGenerateDocument(data) { | |
const parsedData = []; | |
for (let index = 0; index < data.length; index++) { | |
const element = data[index]; | |
if (element.name === 'bacon') { | |
parsedData.push({ | |
name: element.name, | |
age: element.age, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// before named boolean expressions | |
// check if the user is active and if the user has | |
// dependents | |
if (user.status === 'active' && | |
user.dependents.length > 1) { | |
// perform something important | |
} | |
// before | |
if (!user.authenticated) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// before good naming | |
const user = getActiveUser(); | |
const peopleData = fetchData(); | |
function beginProc(data) {} | |
const d = new Date(); | |
class CatRepository { |