Created
December 25, 2023 12:16
-
-
Save SekiT/cfbad7932b7d4323a4c4dbe7530ae4f2 to your computer and use it in GitHub Desktop.
Cyclic Tag System implementation in TypeScript type-level function
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 Step<Input extends string, Program extends string[]> = | |
Program extends [infer ProgramHead extends string, ...infer ProgramTail] | |
? Input extends `0${infer Rest}` | |
? [Rest, [...ProgramTail, ProgramHead]] | |
: Input extends `1${infer Rest}` | |
? [`${Rest}${ProgramHead}`, [...ProgramTail, ProgramHead]] | |
: [Input, Program] | |
: [Input, []]; | |
type StepN<Input extends string, Program extends string[], N extends number, Counter extends 0[] = []> = | |
Counter['length'] extends N | |
? Input | |
: Step<Input, Program> extends [infer NextInput extends string, infer NextProgram extends string[]] | |
? StepN<NextInput, NextProgram, N, [0, ...Counter]> | |
: never; | |
type CTS<Input extends string, Program extends string[]> = StepN<Input, Program, -1>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment