Skip to content

Instantly share code, notes, and snippets.

View jcar787's full-sized avatar

Jeff C. jcar787

View GitHub Profile
@jcar787
jcar787 / hello-world.rs
Created March 27, 2022 01:10
The Infamous Hello World
fn main() {
println!("Meh! Hello World!");
}
@jcar787
jcar787 / on-click-no-closures.jsx
Created March 24, 2022 01:43
create onClick handlers per object without using closures
const fakeAPI = (fakeURL, fakeId) => {
return new Promise((res) => {
setTimeout(() => {
console.log(`Calling our ${fakeURL} with our ${fakeId}`);
res();
}, 1250);
});
};
@jcar787
jcar787 / closures-on-click.jsx
Created March 24, 2022 01:12
Creating onClick handlers per object
const withFakeAPI = (fakeURL) => (fakeId) => (e) => {
e.preventDefault();
return new Promise((res) => {
setTimeout(() => {
console.log(`Calling our ${fakeURL} with our ${fakeId}`);
res();
}, 1250);
});
};
@jcar787
jcar787 / encapsulation-closure.js
Created March 24, 2022 00:45
Privacy by default
function User(name, age) {
const getName = () => name;
const getAge = () => age;
const setName = newName => { name = newName; };
const setAge = newAge => { age = newAge; };
return {
getName,
getAge,
setName,
@jcar787
jcar787 / fake-api-closures.js
Created March 23, 2022 23:56
A more clever example of closures
// Closure version
const withFakeAPI = fakeURL => fakeId => {
return new Promise((res) => {
setTimeout(() => {
console.log(`Calling our ${fakeURL} with our ${fakeId}`);
res();
}, 1250);
});
};
@jcar787
jcar787 / four-levels.js
Created March 23, 2022 23:28
four levels deep in closures
function firstLevel(a) {
return function dungeonLevel(b) {
return function waterLevel(c) {
return function bowserLevel(d) {
return a + b + c + d;
}
}
}
};
@jcar787
jcar787 / my-first-closure.js
Created March 23, 2022 22:47
A simple example using closures.
function outerFunction(name) {
const myName = 'Jeff';
return function innerFunction() {
console.log(`Nice to meet you ${name}. My name is ${myName}`);
}
}
@jcar787
jcar787 / reduce-sequential-async.js
Created March 12, 2022 15:10
Sequential async calls using reduce
const fakeAPICall = (id) => {
const people = [
{name:'Jim Halpert', position:'Sales Person', location: 'PA', department: 'Sales'},
{name:'Dwight Schrute', position:'Sales Person', location: 'PA', department: 'Sales'},
{name:'Pam Beasly', position:'Office Manager', location: 'PA', department: 'HR'},
{name:'Stanley Hudson', position:'Sales Person', location: 'PA', department: 'Sales'},
{name:'Michael Scott', position:'Manager', location: 'PA', department: 'Management'},
{name:'Karen Filipelli', position:'Sales Person', location: 'CT', department: 'Sales'},
{name:'Creed Bratton', position:'QA', location: 'PA', department: 'QA'},
{name:'Jo Bennett', position:'CEO', location: 'FL', department: 'Management'},
@jcar787
jcar787 / reduce-parallel-async.js
Created March 11, 2022 15:18
Parallel async calls using reduce
const fakeAPICall = (id) => {
const people = [
{name:'Jim Halpert', position:'Sales Person', location: 'PA', department: 'Sales'},
{name:'Dwight Schrute', position:'Sales Person', location: 'PA', department: 'Sales'},
{name:'Pam Beasly', position:'Office Manager', location: 'PA', department: 'HR'},
{name:'Stanley Hudson', position:'Sales Person', location: 'PA', department: 'Sales'},
{name:'Michael Scott', position:'Manager', location: 'PA', department: 'Management'},
{name:'Karen Filipelli', position:'Sales Person', location: 'CT', department: 'Sales'},
{name:'Creed Bratton', position:'QA', location: 'PA', department: 'QA'},
{name:'Jo Bennett', position:'CEO', location: 'FL', department: 'Management'},
@jcar787
jcar787 / group-by.js
Created March 9, 2022 15:00
Create a function that accepts an array and a key and return an object grouped by the key
function groupBy(arr, groupByKey) {
return arr.reduce((group, element) => {
const key = element[groupByKey];
if (key in group) {
group[key].push(element);
} else {
group[key] = [element];
}
return group;