<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [Typescript] 45. Medium - MinusOne (Solution to solve max number of iteration by tail call)

      Just for fun...

      Given a number (always positive) as a type. Your type should return the number decreased by one.

      For example:

      type Zero = MinusOne<1> // 0
      type FiftyFour = MinusOne<55> // 54

       

      /* _____________ Your Code Here _____________ */
      type Tuple<L extends number, T extends unknown[] = []> = T["length"] extends L
        ? T
        : Tuple<L, [...T, unknown]>;
      type MinusOne<T extends number> = Tuple<T> extends [...infer L, unknown]
        ? L["length"]
        : never;
      
      /* _____________ Test Cases _____________ */
      import type { Equal, Expect } from '@type-challenges/utils'
      
      type cases = [
        Expect<Equal<MinusOne<1>, 0>>,
        Expect<Equal<MinusOne<55>, 54>>,
        Expect<Equal<MinusOne<3>, 2>>,
        Expect<Equal<MinusOne<100>, 99>>,
        Expect<Equal<MinusOne<1101>, 1100>>, // won't work
      ]

       

      For `Tuple<L>`, we need adding `unknown` into tuple, until the length of tuple is the same as `L`.

      For `MinusOne<L>`, using `Tuple` to accumate the tuple length, then sub one get result.

       

      But the solution has one problem, the number of iterlation might crash the compiler. 

      In function programming, there is a technique called tail call. See this post: http://www.rzrgm.cn/Answer1215/p/16598616.html

      Trampolines is the solution idea to solve the problem. basicly wrap function into another function, inside wrapper, keep calling original function. But since everytime calling the orignial function return a new function, the callstack will be free (size will be 1 or 0), never increase. That solve the max number iteration problem.

      In the Type, we can do the magic by add to the begining:

      0 extends 1 ? never: <...rest of the code>

      /* _____________ Your Code Here _____________ */
      type Tuple<L extends number, T extends unknown[] = []> = 0 extends 1 ? never: T["length"] extends L
        ? T
        : Tuple<L, [...T, unknown]>;
      type MinusOne<T extends number> = Tuple<T> extends [...infer L, unknown]
        ? L["length"]
        : never;
      
      /* _____________ Test Cases _____________ */
      import type { Equal, Expect } from '@type-challenges/utils'
      
      type cases = [
        Expect<Equal<MinusOne<1>, 0>>,
        Expect<Equal<MinusOne<55>, 54>>,
        Expect<Equal<MinusOne<3>, 2>>,
        Expect<Equal<MinusOne<100>, 99>>,
        Expect<Equal<MinusOne<1101>, 1100>>, // works
      ]

       

      Solution 2: 

      type MinusOne<T extends number, ARR extends unknown[] = []> = any extends never ? never: [...ARR, 1]['length'] extends T ? ARR['length'] : MinusOne<T, [...ARR, 1]>
      
      /* _____________ Test Cases _____________ */
      import type { Equal, Expect } from '@type-challenges/utils'
      
      type cases = [
        Expect<Equal<MinusOne<1>, 0>>,
        Expect<Equal<MinusOne<55>, 54>>,
        Expect<Equal<MinusOne<3>, 2>>,
        Expect<Equal<MinusOne<100>, 99>>,
        Expect<Equal<MinusOne<1101>, 1100>>,
      ]

       

      Refer: https://github.com/microsoft/TypeScript/issues/49459

      posted @ 2022-10-07 22:57  Zhentiw  閱讀(64)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 中文字幕一区二区网站| 欧美成人精品手机在线| 日本国产精品第一页久久| 日韩亚洲精品中文字幕| 亚洲欧美日韩综合一区二区| 91中文字幕一区在线| 亚洲男人第一无码av网站| 五月天丁香婷婷亚洲欧洲国产| 在线中文字幕第一页| 国内少妇偷人精品免费| 国产av无码国产av毛片| 国产内射性高湖| 国产果冻豆传媒麻婆精东| 国产国产午夜福利视频| 国内精品久久久久精免费| 国产熟睡乱子伦视频在线播放| 大香蕉av一区二区三区| 国产一区二区三区怡红院| 蜜桃AV抽搐高潮一区二区| xxxx丰满少妇高潮| 少妇人妻av无码专区| 九九成人免费视频| 亚洲欧洲日产国码无码久久99| 精品无码久久久久久久久久| 国产99久久精品一区二区| 国产区成人精品视频| 久久久久青草线蕉亚洲| 久爱www人成免费网站| 后入内射无码人妻一区| 精品无码人妻一区二区三区| 国产AV福利第一精品| 扒开粉嫩的小缝隙喷白浆视频| 日本午夜精品一区二区三区电影| 中文字幕 日韩 人妻 无码| 久久综合精品成人一本| 国内精品久久久久影院网站| 精品偷拍一区二区三区在| 国产精品中文字幕综合| 青草青草久热国产精品| 欧美粗大| 最新国产精品中文字幕|