Skip to content

Instantly share code, notes, and snippets.

👌
livin' the dream...

Dave Lunny himynameisdave

👌
livin' the dream...
Block or report user

Report or block himynameisdave

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@himynameisdave
himynameisdave / reduce-to-string.js
Last active May 21, 2017
Reduce your fears about .reduce() - Reduce array to single string
View reduce-to-string.js
/*
Naive implementation
*/
let everyonesName = '';
users.forEach(user => {
everyonesName += `${user.firstName} ${user.lastName}\n`;
});
/*
Better one-line-using-reduce implementation
@himynameisdave
himynameisdave / array-find-reduce_03.js
Last active May 21, 2017
Reduce your fears about .reduce() - Array.find with reduce 03
View array-find-reduce_03.js
// arrayFind accepts an array and returns a function
// the returned function accepts the finder function
const arrayFind = arr => fn => arr.reduce((acc, item, index) => {
// We pass the finder function the item and the index
if (fn(item, index)) return item;
return acc;
});
// Creates a finder function for just our fruits
const fruitFinder = arrayFind(fruits);
// Now we can pass a finder function to fruitFinder (seen as `fn` above)
@himynameisdave
himynameisdave / array-find-reduce_02.js
Created May 21, 2017
Reduce your fears about .reduce() - Array.find with reduce 02
View array-find-reduce_02.js
const thisShitIsBananas = fruits.reduce((accumulator, fruit) => {
if (fruit.name === 'bananas') return fruit;
return accumulator
});
@himynameisdave
himynameisdave / array-find-reduce_01.js
Created May 21, 2017
Reduce your fears about .reduce() - Array.find with reduce 01
View array-find-reduce_01.js
const fruits = [
  { name: ‘apples’, quantity: 2 },
  { name: ‘bananas’, quantity: 0 },
  { name: ‘cherries’, quantity: 5 }
];
const thisShitIsBananas = fruits.reduce((accumulator, fruit) => {
  return accumulator
});
@himynameisdave
himynameisdave / twentySomethingsLongFullNames-reduce.js
Last active May 21, 2017
Reduce your fears about .reduce() - twentySomethingsLongFullNames-reduce.js
View twentySomethingsLongFullNames-reduce.js
const isInTwenties = user => user.age >= 20 && user.age < 30;
const makeFullName = user => `${user.firstName} ${user.lastName}`;
const isAtLeastTenChars = fullName => fullName.length >= 10;
const twentySomethingsLongFullNames = users.reduce(
// The first arg that .reduce takes is the reducer function
// It has the signature:
// - `accumulator` - this is what you build up as you move through the array
// - `user` - this is the reference to the item in the array we are currently on
// - `index` - not used here, but you can get the index of the current place in the array you are
@himynameisdave
himynameisdave / twentySomethingsLongFullNames.js
Created May 20, 2017
Reduce your fears about .reduce() - twentySomethingsLongFullNames.js
View twentySomethingsLongFullNames.js
const isInTwenties = user => user.age >= 20 && user.age < 30;
const makeFullName = user => `${user.firstName} ${user.lastName}`;
const isAtLeastTenChars = fullName => fullName.length >= 10;
const twentySomethingsLongFullNames = users.filter(isInTwenties)
.map(makeFullName)
.filter(isAtLeastTenChars);
@himynameisdave
himynameisdave / twentySomethingsLongFullNames-anonymous.js
Last active May 20, 2017
Reduce your fears about .reduce() - twentySomethingsLongFullNames-anonymous.js
View twentySomethingsLongFullNames-anonymous.js
const twentySomethingsLongFullNames = users
// First we filter only the users who are in their twenties
.filter(user => user.age >= 20 && user.age < 30)
// Combine first and last names
.map(user => `${user.firstName} ${user.lastName}`)
// Now remove any names that are 9 or less characters
.filter(fullName => fullName.length >= 10);
@himynameisdave
himynameisdave / users.js
Created May 20, 2017
Reduce your fears about .reduce() - Sample Data Set
View users.js
const users = [
{
firstName: 'Bob',
lastName: 'Doe',
age: 37,
}, {
firstName: 'Rita',
lastName: 'Smith',
age: 21,
}, {
@himynameisdave
himynameisdave / keybase.md
Created Jan 19, 2017
Keybase GitHub Proof
View keybase.md

Keybase proof

I hereby claim:

  • I am himynameisdave on github.
  • I am himynameisdave (https://keybase.io/himynameisdave) on keybase.
  • I have a public key ASCl_Zfa6dYXlVzsnNxrhI8J7WxWkVPB9z8eF8tKmYQQNwo

To claim this, I am signing this object:

@himynameisdave
himynameisdave / link-underline-mixin.scss
Created Jan 9, 2017
A mixin for a stylish underline text effect
View link-underline-mixin.scss
You can’t perform that action at this time.