Created
June 21, 2023 18:31
-
-
Save chrismay/959042bdcf01d6b9c45224488bd8070d to your computer and use it in GitHub Desktop.
Preserve tuple length when mapping
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
interface ReadonlyArray<T> { | |
map<U>( | |
callbackfn: (value: T, index: number, array: T[]) => U, | |
thisArg?: any | |
): { [K in keyof this]: U }; | |
} | |
type Coord = readonly [number, number, number]; | |
type BiFunction<T> = (a: T, b: T) => T; | |
function map(a: Coord, f: (i: number) => number): Coord { | |
return a.map(f); | |
} | |
function zip(a: Coord, b: Coord, f: BiFunction<number>): Coord { | |
return a.map((v, i) => f(v, b[i])); | |
} | |
var moved = zip([0, 1, 2], [1, 1, 1], (a, b) => a + b); | |
console.log(moved); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment