hkt-toolbelt icon indicating copy to clipboard operation
hkt-toolbelt copied to clipboard

Apply tail-call rec to List.FlattenN + write stress tests

Open poteat opened this issue 1 year ago • 0 comments

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.

poteat avatar Aug 08 '23 21:08 poteat