Skip to content

Instantly share code, notes, and snippets.

@iandesj
iandesj / reduce_examples.js
Last active November 14, 2019 13:54
Examples of reduce() vs for loop
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);
@iandesj
iandesj / map_examples.js
Last active November 14, 2019 13:51
Examples of map() vs for loop
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ];
console.log('map() example');
const upperCaseFruits = fruits.map((value) => {
return value.toUpperCase();
});
console.log('upperCaseFruits =', upperCaseFruits, '\n');
@iandesj
iandesj / filter_examples.js
Last active November 14, 2019 13:46
Examples of filter() vs for loop
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')) {
@iandesj
iandesj / find_example.js
Last active November 14, 2019 13:43
Examples of find() vs for loop
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')) {
@iandesj
iandesj / foreach_example.js
Created October 11, 2019 17:47
Examples of foreach() vs for loop
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]);
@iandesj
iandesj / my_git_branches.py
Created October 1, 2019 12:32
Just get all branches pushed to all remote repos with me as the committer.
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)
@iandesj
iandesj / parameters.js
Created July 16, 2019 14:45
Configuration Objects vs. Many Parameters
// before - 5 args too many
function initializeNewUserWorflow(
email, password, location,
organization, referred) {
// initialize the new user
}
// after - using configuration object
const initializeUserConfiguration = {
email: 'email',
@iandesj
iandesj / methods.js
Created July 16, 2019 14:43
Simplified Methods
// 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,
});
@iandesj
iandesj / booleans.js
Created July 16, 2019 14:40
Named Boolean Expressions
// 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) {
@iandesj
iandesj / naming.js
Created July 16, 2019 14:39
Variable & Function Naming
// before good naming
const user = getActiveUser();
const peopleData = fetchData();
function beginProc(data) {}
const d = new Date();
class CatRepository {