Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

216 - Slice #18720

Open
alexfung888 opened this issue Nov 7, 2022 · 0 comments
Open

216 - Slice #18720

alexfung888 opened this issue Nov 7, 2022 · 0 comments
Labels
216 answer Share answers/solutions to a question en in English

Comments

@alexfung888
Copy link

// remove the tail starting with End (non-negative)
// Head is the substring starting from Arr[0]
// Tail is the rest of it
// therefore cut when Head['length'] is End
// notice the Head/Tail here are not the same as the Head/Tail in my caller (Slice3<>).
type Cut<
  Arr extends number[], End extends number, 
  Head extends number[], Tail extends number[], R extends number[] = []
> = End extends Head['length']
  ? R
  : Tail extends [infer A1 extends number, ...infer T extends number[]]
    ? Cut<Arr, End, [...Head, A1], T, [...R, A1]>
    : []
;

// Start and End are non-negative
// find the substring starting at Start and pass to Cut<> to remove tail
// Head is a substring starting from Arr[0], growing from []
// Tail is the rest of Arr[], starting with the whole array
// The final result is a left$() of Tail
type Slice3<
  Arr extends number[], Start extends number = 0, End extends number = Arr['length'], 
  Head extends number[] = [], Tail extends number[] = Arr
> = Start extends Head['length']
  ? Cut<Arr, End, Head, Tail>
  : Tail extends [infer A1 extends number, ...infer T extends number[]]
    ? Slice3<Arr, Start, End, [...Head, A1], T>
    : [];

// convert negative length to positive equivalent
// Input: Length > 0 (negative sign already removed)
// Output: Arr['length'] - Length
type N2P<Arr extends number[], Length extends number, R extends number[] = [], L extends unknown[] = []> = Arr extends [...infer T extends number[], infer A9 extends number]
  ? Length extends R['length']
    ? Arr['length']
    : N2P<T, Length, [A9, ...R]>
  : L['length'];

// handle negative indexes to always return a non-negative index
// leave positive indexes untouched
type UnNeg<Arr extends number[], Length extends number> = `${Length}` extends `-${infer S extends number}`
  ? N2P<Arr, S>
  : Length;

type Slice<
  Arr extends number[], Start extends number = 0, End extends number = Arr['length'], 
  Head extends number[] = [], Tail extends number[] = Arr
> = Slice3<Arr, UnNeg<Arr, Start>, UnNeg<Arr, End>, Head, Tail>// your answers
@alexfung888 alexfung888 added answer Share answers/solutions to a question en in English labels Nov 7, 2022
@github-actions github-actions bot added the 216 label Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
216 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant