Last active
August 22, 2023 09:29
-
-
Save sebinsua/5441536ac5bbbce444f44c8f57d8fb8e to your computer and use it in GitHub Desktop.
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 Zipped<A extends readonly (readonly any[])[]> = { | |
[RowIndex in keyof A[0]]: RowIndex extends "length" ? number : { | |
[ArrayIndex in keyof A]: A[ArrayIndex] extends readonly any[] ? A[ArrayIndex][RowIndex] : never; | |
}; | |
}; | |
type NumericRange<TotalLength extends number, TempRange extends any[] = []> = | |
TempRange['length'] extends TotalLength ? TempRange : NumericRange<TotalLength, [...TempRange, TempRange['length']]>; | |
type TupleFromKeys<T, K extends readonly (keyof T)[]> = { | |
[I in keyof K]: K[I] extends keyof T ? T[K[I]] : never; | |
}; | |
type FixedSequence<T extends readonly (readonly any[])[]> = TupleFromKeys<Zipped<T>, NumericRange<T[0]['length']>>; | |
function zip<const T extends readonly (readonly any[])[]>(...arrays: T): FixedSequence<T> { | |
const minLength = Math.min(...arrays.map(arr => arr.length)); | |
const result: any[][] = []; | |
for (let i = 0; i < minLength; i++) { | |
const zippedItem = arrays.map(arr => arr[i]); | |
result.push(zippedItem); | |
} | |
return result as FixedSequence<T>; | |
} | |
const grid = [ | |
['a', 1, true], | |
['b', 2, false], | |
['c', 3, true] | |
] as const; | |
const [col1, col2, col3] = zip(...grid); | |
console.log(col1); | |
console.log(col2); | |
console.log(col3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
zip
ing values with a range is quite a fun way of enumerating that appears to come from Haskell.See: https://www.typescriptlang.org/play?#code/PTAEHUFNQYwC0jA1qA7ggLggTqABgCp6gCWAzqAIagB2ArgLaTYkygA2JGzl7oARgE9YCZCRoBzUFkoZ89Bv2Z4AUCFAATAPaQKNLXMgAPbjQ2gAZti0N8RAXTky5hYtt2hjp81ZvzGSth4AHQqGIIADtAAkmQAcozMrAAyXDzsADwEAHygALy0AcyeJpBm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