Skip to content

Instantly share code, notes, and snippets.

@chrismay
Created June 21, 2023 18:31
Show Gist options
  • Save chrismay/959042bdcf01d6b9c45224488bd8070d to your computer and use it in GitHub Desktop.
Save chrismay/959042bdcf01d6b9c45224488bd8070d to your computer and use it in GitHub Desktop.
Preserve tuple length when mapping
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