This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const space = (w1, w2) => `${w1} ${w2}`; | |
space('hello', 'world'); // => "hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const p1 = [1, 2]; | |
const p2 = [1, 2]; | |
const p3 = [1, 3]; | |
p2[1] = 3; | |
// Which of p1, p2 and p3 are "the same"? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const p1 = [1, 2] as const; | |
const p2 = [1, 2] as const; | |
const p3 = [1, 3] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum PrimaryColor { | |
Red = "RED", | |
Yellow = "YELLOW", | |
Blue = "BLUE" | |
} | |
const green1 = [ | |
{ color: PrimaryColor.Yellow, weight: 1 / 2 }, | |
{ color: PrimaryColor.Blue, weight: 1 / 2 } | |
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let name = 'george'; | |
name = 'jerry'; |