Last active
August 19, 2019 22:16
-
-
Save jbmilgrom/47979e50b2ad4095720f0f7528a69e1d to your computer and use it in GitHub Desktop.
example of "sameness" in the context of nested immutable data (> TS 3.4 needed @see https://devblogs.microsoft.com/typescript/announcing-typescript-3-4/#improvements-for-readonlyarray-and-readonly-tuples)
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]); | |
const areNumberPairsEqual = (a: NumberPair, b: NumberPair) => | |
arePairsEqual(a, b, areNumbersEqual); | |
const arePairsOfNumberPairsEqual = ( | |
a: PairOfNumberPairs, | |
b: PairOfNumberPairs | |
) => arePairsEqual(a, b, areNumberPairsEqual); | |
arePairsOfNumberPairsEqual([[1, 2], [3, 4]], [[1, 2], [3, 4]]); // true | |
arePairsOfNumberPairsEqual([[1, 2], [3, 4]], [[1, 2], [3, 5]]); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment