You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed some anwser uses 0 extends 1 to bypass this limitation. It says it's for tail-recursive optimization, but it's not indeed, it's just a hack or bug for now, so I'll not consider this way for the moment.
We could try to increase the steps in each recursion to reduce the times of recursion.
Short Version
With the hint above, I made the following enhanced solution:
It just checks and increases two items instead one in each recursion, so to compute 1101, now it only needs 1101/2=551 recursions, which is less than 1000 limititation.
Long Version
Think it further, the step of <2> is hard coded into the solution. Can we parameterize it so we can easily change it to the max limitation of the typescript compiler without writing 999 extra lines of hard code?
The solution below is just for this purpose.
// normal tuple of given size with linear complexitytypeTuple<Lextendsnumber,Textendsunknown[]=[]>=T["length"]extendsL
? T
: Tuple<L,[...T,unknown]>;// find the tuple of given size between From and To tuples// if not found, return nevertypeFromToTuple<Lextendsnumber,Fromextendsunknown[]=[],Toextendsunknown[]=[]>=From['length']extendsL
? From
: From['length']extendsTo['length']
? never
: FromToTuple<L,[unknown, ...From],To>// prepare a tuple as increament of each recursiontypeTupleMax=Tuple<999>// build a tuple of given size// it use a large step in each recursion, so it can reach the target with less recursionstypeSuperTuple<Lextendsnumber,Cextendsunknown[]=[],Pextendsunknown[]=[]>=C["length"]extendsL
? C
: FromToTuple<L,P,C>extendsnever
? SuperTuple<L,[...TupleMax, ...C],C>
: FromToTuple<L,P,C>// this is just a middle abstraction to make the final algorithm more cleartypeMinus<Aextendsnumber,Bextendsnumber>=SuperTuple<A>extends[ ...SuperTuple<B>, ... infer Rextendsunknown[]] ? R['length'] : nevertypeMinusOne<Textendsnumber>=Textends0
? -1
: Minus<T,1>
Other idea
The core idea of my solutions and similar solutions are to count items in tuple. There is another branch of idea for this challenge. Look at this issue: #2586
The core idea of that is to map a single number literal to a tuple of size of that number, and multiple it 10 times to match the level of the number.
It's more effective and not that difficult to understand. What a colorful world.
More, I found this issue: #13507 it is the most fast and supports the biggest number.
The text was updated successfully, but these errors were encountered:
Interesting challenge.
中文笔记:https://juejin.cn/post/7167664060547203108/
Short Version with Error ❌
This version passes all cases but the
Expect<Equal<MinusOne<1101>, 1100>>
. Though it's a good start to reason.We increase the tuple items one by one to check if the length matches. When it does, we returns it's length.
It is tail-recursive indeed, so it passes the test for 50 items, but it fails the test for 1101 items, since there is also a limit of 1000 for tail-recursive case.
I noticed some anwser uses
0 extends 1
to bypass this limitation. It says it's for tail-recursive optimization, but it's not indeed, it's just a hack or bug for now, so I'll not consider this way for the moment.We could try to increase the steps in each recursion to reduce the times of recursion.
Short Version
With the hint above, I made the following enhanced solution:
It just checks and increases two items instead one in each recursion, so to compute 1101, now it only needs 1101/2=551 recursions, which is less than 1000 limititation.
Long Version
Think it further, the step of <2> is hard coded into the solution. Can we parameterize it so we can easily change it to the max limitation of the typescript compiler without writing 999 extra lines of hard code?
The solution below is just for this purpose.
Other idea
The core idea of my solutions and similar solutions are to count items in tuple. There is another branch of idea for this challenge. Look at this issue: #2586
The core idea of that is to map a single number literal to a tuple of size of that number, and multiple it 10 times to match the level of the number.
It's more effective and not that difficult to understand. What a colorful world.
More, I found this issue: #13507 it is the most fast and supports the biggest number.
The text was updated successfully, but these errors were encountered: