Skip to content

Instantly share code, notes, and snippets.

View ericelliott's full-sized avatar
💭
https://leanpub.com/composingsoftware

Eric Elliott ericelliott

💭
https://leanpub.com/composingsoftware
View GitHub Profile
@ericelliott
ericelliott / declarative-example.js
Last active June 10, 2017 06:52
Declarative example.
const doubleMap = numbers => numbers.map(n => n * 2);
console.log(doubleMap([2, 3, 4])); // [4, 6, 8]
@ericelliott
ericelliott / imperative-example.js
Last active June 10, 2017 06:52
Imperative example
const doubleMap = numbers => {
const doubled = [];
for (let i = 0; i < numbers.length; i++) {
doubled.push(numbers[i] * 2);
}
return doubled;
};
console.log(doubleMap([2, 3, 4])); // [4, 6, 8]
@ericelliott
ericelliott / frozen-not-immutable.js
Created January 3, 2017 21:35
Frozen, not immutable
const a = Object.freeze({
foo: { greeting: 'Hello' },
bar: 'world',
baz: '!'
});
a.foo.greeting = 'Goodbye';
console.log(`${ a.foo.greeting }, ${ a.bar }${a.baz}`);
@ericelliott
ericelliott / frozen-objects.js
Created January 3, 2017 21:23
Frozen object
const a = Object.freeze({
foo: 'Hello',
bar: 'world',
baz: '!'
});
a.foo = 'Goodbye';
// Error: Cannot assign to read only property 'foo' of object Object
@ericelliott
ericelliott / no-timing-dependency.js
Last active April 4, 2022 20:01
Pure functions have no timing dependency issues.
const x = {
val: 2
};
const x1 = x => Object.assign({}, x, { val: x.val + 1});
const x2 = x => Object.assign({}, x, { val: x.val * 2});
console.log(x1(x2(x)).val); // 5
@ericelliott
ericelliott / timing-dependency.js
Created January 3, 2017 00:42
Timing dependency
// With shared state, the order in which function calls are made
// changes the result of the function calls.
const x = {
val: 2
};
const x1 = () => x.val += 1;
const x2 = () => x.val *= 2;
@ericelliott
ericelliott / promise-example.js
Created December 10, 2016 02:03
Promise example
const doSomething = (err) => new Promise((resolve, reject) => {
if (err) return reject(err);
setTimeout(() => {
resolve(42);
}, 1000);
});
const log = value => console.log(value);
@ericelliott
ericelliott / rxjs-patching.js
Created December 10, 2016 00:35
Reduce bundle size with RxJS patching
import { Observable } from 'rxjs/Observable';
// then patch import only needed operators:
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/from';
const foo = Observable.from([1, 2, 3]);
foo.map(x => x * 2).subscribe(n => console.log(n));
@ericelliott
ericelliott / dev-vs-production-type-checking.js
Last active December 7, 2021 22:33
Dev vs production type checking
const check = fn => (params = []) => {
const { required } = fn;
const missing = required.filter(param => !(param in params));
if (missing.length) {
throw new Error(`${ fn.name }() Missing required parameter(s):
${ missing.join(', ') }`);
}
return fn(params);
@ericelliott
ericelliott / naive-runtime-type-checking.js
Last active September 1, 2022 02:56
Naive runtime type checking example
const fn = (fn, {required = []}) => (params = {}) => {
const missing = required.filter(param => !(param in params));
if (missing.length) {
throw new Error(`${ fn.name }() Missing required parameter(s):
${ missing.join(', ') }`);
}
return fn(params);
};