Skip to content

Instantly share code, notes, and snippets.

Jonathan Milgrom jbmilgrom

Block or report user

Report or block jbmilgrom

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jbmilgrom
jbmilgrom / pair.immutable.ts
Created Aug 28, 2019
"sameness" in the context of immutability
View pair.immutable.ts
const p1 = [1, 2] as const;
const p2 = [1, 2] as const;
const p3 = [1, 3] as const;
@jbmilgrom
jbmilgrom / twoColors.immutable.ts
Created Aug 27, 2019
"Sameness" in the context of immutability
View twoColors.immutable.ts
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 Sep 23, 2019
Example of "sameness" in the context of mutability
View areAccountsEqual.mutable.ts
/**
* 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
View arePairsOfPairsEqual.immutable.ts
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]);
View arePairsEqual.immutable.ts
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 Aug 18, 2019
Example of mutable binding, generally.
View name.mutable.js
let name = 'george';
name = 'jerry';
@jbmilgrom
jbmilgrom / profiles.immutable.ts
Last active Aug 18, 2019
Example of arbitrary immutable data
View profiles.immutable.ts
const profiles = [
{
lastName: "Curry",
firstName: "Steph",
team: "WARRIORS"
},
{
lastName: "James",
firstName: "Lebron",
team: "LAKERS",
@jbmilgrom
jbmilgrom / colors.immutable.ts
Created Aug 17, 2019
Example of arbitrary immutable data
View colors.immutable.ts
enum PrimaryColor {
Red = "RED",
Yellow = "YELLOW",
Blue = "BLUE"
}
const green = [
{ color: PrimaryColor.Yellow, weight: 1 / 2 },
{ color: PrimaryColor.Blue, weight: 1 / 2 }
] as const;
@jbmilgrom
jbmilgrom / mutableNumber.invalid.js
Last active Sep 21, 2019
Imagined API for mutating numbers
View mutableNumber.invalid.js
const HOURS_IN_DAY = 24;
/**
* NOT VALID; imagined API for mutating numbers
*/
24.increment();
HOURS_IN_DAY + 1 === 26;
@jbmilgrom
jbmilgrom / decrementOneHundred.immutable.js
Last active Aug 16, 2019
Example of use of immutable binding
View decrementOneHundred.immutable.js
const oneHundred = 100; // <-- now a `const` instead of a `let`
const decrementOneHundred = x => oneHundred - x;
decrementOneHundred(20); // 80
// oneHundred = 80; <-- would be runtime error: "TypeError: Assignment to constant variable."
decrementOneHundred(20); // 80
You can’t perform that action at this time.