hkt-toolbelt
hkt-toolbelt copied to clipboard
Apply tail-call rec to List.FlattenN + write stress tests
In the following code:
type _$flattenN2<
T extends unknown[],
N extends DigitList.DigitList,
N_NEXT extends DigitList.DigitList = DigitList._$decrement<N>,
RESULT extends List.List = N extends [Digit.Zero]
? T
: _$flattenN2<_$flattenShallow<T>, N_NEXT>
> = RESULT
type _$flattenShallow<
T extends unknown[],
RESULT extends List.List = T extends [infer H, ...infer R]
? H extends unknown[]
? [...H, ..._$flattenShallow<R>]
: [H, ..._$flattenShallow<R>]
: []
> = RESULT
The following clauses violate rules around tail-call rec optimization:
-
[...H, ..._$flattenShallow<R>]
-
[H, ..._$flattenShallow<R>]
I think this can be written to be more performant thereby - as well, adding stress tests would validate this.