Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
https://leanpub.com/composingsoftware

Eric Elliott ericelliott

💭
https://leanpub.com/composingsoftware
View GitHub Profile
@ericelliott
ericelliott / getenv.js
Created Nov 9, 2012
dump environment settings to console from node
View getenv.js
var env = process.env;
Object.keys(env).forEach(function(key) {
console.log('export ' + key + '="' + env[key] +'"');
});
View essential-javascript-links.md
@ericelliott
ericelliott / lambda-not-anon.md
Last active Sep 27, 2020
The distinction between anonymous functions and lambdas in JavaScript.
View lambda-not-anon.md

TL;DR - Lambda means "function expression used as data".

Anonymous function means "function without a name".

This is one of the relatively few cases where the Wikipedia definition of a word, while not entirely wrong, is misleading. Lambdas and anonymous functions are distinct ideas.

These ideas are commonly confused because in many programming languages (and lambda calculus) all lambdas are anonymous or vise verse.

In JavaScript, not all lambdas are anonymous, and not all anonymous functions are used as lambdas, so the distinction has some practical meaning.

@ericelliott
ericelliott / .gitignore
Created Nov 15, 2016
Sample Node project .gitignore
View .gitignore
node_modules
build
npm-debug.log
.env
.DS_Store
@ericelliott
ericelliott / defaults-overrides.md
Last active Sep 8, 2020
ES6 defaults / overrides pattern
View defaults-overrides.md

ES6 Defaults / Overrides Pattern

Combine default parameters and destructuring for a compact version of the defaults / overrides pattern.

function foo ({
    bar = 'no',
    baz = 'works!'
  } = {}) {
View double-list-imperative.js
const doubleList = list => {
const newList = [];
for (var i = 0; i < list.length; i++) {
newList[i] = list[i] * 2;
}
return newList;
};
@ericelliott
ericelliott / parallel-fetch.js
Created Apr 13, 2020
Parallel requests example
View parallel-fetch.js
// setup
const wait = value => new Promise(resolve => {
setTimeout(() => resolve(value), 3000);
});
const fetchFoo = () => wait('foo');
const fetchBar = () => wait('bar');
const fetchBaz = () => wait('baz');
const fetchDataSlowly = async time => {
@ericelliott
ericelliott / use-local-storage.js
Created Mar 30, 2020
A localStorage drop-in replacement for useState
View use-local-storage.js
import { useState } from 'react';
const configureLocalStorage = key => initialValue => {
const [state, setState] = useState(() => {
try {
const value = localStorage.getItem(key);
return value ? JSON.parse(value) : initialValue;
} catch (e) {
console.log(e);
return initialValue;
View composition-example.js
// Composition Example
// http://codepen.io/ericelliott/pen/XXzadQ?editors=001
// https://gist.github.com/ericelliott/fed0fd7a0d3388b06402
const distortion = { distortion: 1 };
const volume = { volume: 1 };
const cabinet = { cabinet: 'maple' };
const lowCut = { lowCut: 1 };
const inputLevel = { inputLevel: 1 };
@ericelliott
ericelliott / wait.js
Created Jan 19, 2017
Wait -- an ES6 promise example
View wait.js
const wait = time => new Promise((resolve) => setTimeout(resolve, time));
wait(3000).then(() => console.log('Hello!')); // 'Hello!'
You can’t perform that action at this time.