Skip to content

Instantly share code, notes, and snippets.

View jbmilgrom's full-sized avatar

Jonathan Milgrom jbmilgrom

View GitHub Profile
@jbmilgrom
jbmilgrom / strings.immutable.js
Last active September 22, 2019 21:19
Arbitrary data can be the subject of functional behavior
const space = (w1, w2) => `${w1} ${w2}`;
space('hello', 'world'); // => "hello world"
@jbmilgrom
jbmilgrom / rationalNumbers.mutable.ts
Last active September 18, 2019 16:21
"sameness" in the context of mutability
type RationalNumber = [number /* numerator */, number /* denominator */];
// turn into decimal form before comparing in order to reduce fraction
const isEqual = (a: RationalNumber, b: RationalNumber) => (a[0] / a[1]) === (b[0] / b[1]);
const r1: RationalNumber = [2, 3];
const r2: RationalNumber = [2, 3];
const r3: RationalNumber = [2, 5];
r2[1] = 5;
@jbmilgrom
jbmilgrom / rationalNumbers.immutable.ts
Last active September 18, 2019 16:24
"sameness" in the context of immutability
type RationalNumber = readonly [number /* numerator */, number /* denominator */];
// turn into decimal form before comparing in order to reduce fraction
const isEqual = (a: RationalNumber, b: RationalNumber) => (a[0] / a[1]) === (b[0] / b[1]);
const r1: RationalNumber = [2, 3];
const r2: RationalNumber = [2, 3];
const r3: RationalNumber = [2, 5];
isEqual(r1, r2); // => true
@jbmilgrom
jbmilgrom / pair.mutable.js
Created August 28, 2019 21:44
Example of "sameness" in the context of mutability
const p1 = [1, 2];
const p2 = [1, 2];
const p3 = [1, 3];
p2[1] = 3;
// Which of p1, p2 and p3 are "the same"?
@jbmilgrom
jbmilgrom / pair.immutable.ts
Created August 28, 2019 13:47
"sameness" in the context of immutability
const p1 = [1, 2] as const;
const p2 = [1, 2] as const;
const p3 = [1, 3] as const;
@jbmilgrom
jbmilgrom / twoColors.immutable.ts
Created August 27, 2019 20:41
"Sameness" in the context of immutability
enum PrimaryColor {
Red = "RED",
Yellow = "YELLOW",
Blue = "BLUE"
}
const green1 = [
{ color: PrimaryColor.Yellow, weight: 1 / 2 },
{ color: PrimaryColor.Blue, weight: 1 / 2 }
] as const;
@jbmilgrom
jbmilgrom / areAccountsEqual.mutable.ts
Last active September 23, 2019 16:48
Example of "sameness" in the context of mutability
/**
* With two _objects_, check equality by checking whether they are in fact the same object i.e. the reference.
*/
const areAccountsEqual = (a: BankAccount, b: BankAccount) => a === b;
const elainesAccount = new BankAccount(100);
const georgesAccount = new BankAccount(100);
elainesAccount.checkBalance(); // 100
georgesAccount.checkBalance(); // 100
type Pair<T> = readonly [T, T];
type NumberPair = Pair<number>;
type PairOfNumberPairs = Pair<NumberPair>;
const areNumbersEqual = (a: number, b: number) => a === b;
const arePairsEqual = <T>(
a: Pair<T>,
b: Pair<T>,
isEqual: (a: T, b: T) => boolean
) => isEqual(a[0], b[0]) && isEqual(a[1], b[1]);
type NumberPair = readonly [number, number];
const areNumbersEqual = (a: number, b: number) => a === b;
const areNumberPairsEqual = (a: NumberPair, b: NumberPair) =>
areNumbersEqual(a[0], b[0]) && areNumbersEqual(a[1], b[1]);
const p1 = [1, 2] as const;
const p2 = [1, 2] as const;
const p3 = [1, 3] as const;
@jbmilgrom
jbmilgrom / name.mutable.js
Created August 18, 2019 20:53
Example of mutable binding, generally.
let name = 'george';
name = 'jerry';