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 (clean, short solution using default params) #21502

Open
MajorLift opened this issue Jan 4, 2023 · 0 comments
Open

216 - Slice (clean, short solution using default params) #21502

MajorLift opened this issue Jan 4, 2023 · 0 comments
Labels
216 answer Share answers/solutions to a question en in English

Comments

@MajorLift
Copy link
Contributor

MajorLift commented Jan 4, 2023

/** 
 * @typedef Slice
 * @params START_POS?, END_POS? - START, END converted to positive indices
 * @returns T.slice(START_POS, END_POS)
 */
type Slice<
  T extends unknown[], 
  START extends number = 0, 
  END extends number = T["length"],
  START_POS extends number = IsNonNegative<START> extends true 
    ? START
    : Subtract<T["length"], Absolute<START>>,
  END_POS extends number = IsNonNegative<END> extends true 
    ? Subtract<T["length"], END>
    : Absolute<END>,
  RESULT = OutOfBounds<START_POS, END_POS, [0, T["length"]]> extends true 
    ? [] 
    : Shift<Pop<T, END_POS>, START_POS>,
  > = RESULT;
/** @returns true if I, J are not numbers or [I, J) is unbounded by [RANGE[0], RANGE[1]) */
type OutOfBounds<LO extends number | never, HI extends number | never, RANGE extends number[]> = 
  LO extends never ? true : HI extends never ? true
    : LessThan<LO, RANGE[0]> extends true ? true 
    : LessThan<RANGE[1], HI> extends true ? true : false;
type IntADT = number | string | bigint;

type Signum<N extends IntADT> = 
  `${N}` extends '0' ? 0
    : `${N}` extends `-${infer _}` ? -1 : 1;

type IsNegative<N extends IntADT> = 
  Signum<N> extends -1 ? true : false;

type IsNonNegative<N extends IntADT> = 
  IsNegative<N> extends false ? true : false;

type Absolute<N extends number> = 
  `${N}` extends `-${infer R extends number}` ? R : N;

type Shift<T extends any[], N extends number = 1> = 
  N extends 0 ? T 
    : T extends [infer _, ...infer Rest]
      ? Shift<Rest, Subtract<N, 1>>
      : [];
      
type Pop<T extends any[], N extends number = 1> = 
  N extends 0
    ? T
    : T extends [...infer Rest, infer _]
      ? Pop<Rest, Subtract<N, 1>>
      : [];

type Subtract<M extends number, S extends number> = 
  Repeat<M> extends [...Repeat<S>, ...infer Rest] ? Rest["length"] : never;

type Repeat<N extends number, T extends unknown = null, M extends T[] = []> = 
  M["length"] extends N ? M : Repeat<N, T, [...M, T]>;

type LessThan<T extends number, U extends number> = 
  Equal<T, U> extends true ? false
    : Subtract<T, U> extends never ? true : false;
@MajorLift MajorLift added answer Share answers/solutions to a question en in English labels Jan 4, 2023
@github-actions github-actions bot added the 216 label Jan 4, 2023
@MajorLift MajorLift changed the title 216 - Slice (most intuitive approach) 216 - Slice (clean, short solution using default params) Jan 6, 2023
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