Skip to content

Instantly share code, notes, and snippets.

View himynameisdave's full-sized avatar
✌️
livin' the dream...

Dave Lunny himynameisdave

✌️
livin' the dream...
View GitHub Profile
@himynameisdave
himynameisdave / JEOPARDY_QUESTIONS1
Created September 25, 2020 21:26 — forked from BROWNPROTON/JEOPARDY_QUESTIONS1
200,000+ Jeopardy! Questions in a JSON file
URL: http://skeeto.s3.amazonaws.com/share/JEOPARDY_QUESTIONS1.json.gz
The json file is an unordered list of questions where each question has
'category' : the question category, e.g. "HISTORY"
'value' : $ value of the question as string, e.g. "$200"
Note: This is "None" for Final Jeopardy! and Tiebreaker questions
@himynameisdave
himynameisdave / exchange-rate.sh
Created June 25, 2020 22:16
Bash script to scrape Google for the current USD-to-CAD exchange rate
#!/bin/bash
# Variables (feel free to edit these)
FROM_CURRENCY='usd'
TO_CURRENCY='cad'
# Constants
USER_AGENT="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
SEARCH_URL="https://www.google.com/search?hl=en&q=$FROM_CURRENCY%20to%20$TO_CURRENCY"
@himynameisdave
himynameisdave / reduce-to-string.js
Last active May 21, 2017 21:29
Reduce your fears about .reduce() - Reduce array to single string
/*
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 21:26
Reduce your fears about .reduce() - Array.find with reduce 03
// 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 20:31
Reduce your fears about .reduce() - Array.find with reduce 02
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 20:05
Reduce your fears about .reduce() - Array.find with reduce 01
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 19:37
Reduce your fears about .reduce() - 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 21:33
Reduce your fears about .reduce() - 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 21:30
Reduce your fears about .reduce() - 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 21:20
Reduce your fears about .reduce() - Sample Data Set
const users = [
{
firstName: 'Bob',
lastName: 'Doe',
age: 37,
}, {
firstName: 'Rita',
lastName: 'Smith',
age: 21,
}, {