[Typescript] 67. Medium - Chunk
Do you know lodash
? Chunk
is a very useful function in it, now let's implement it. Chunk<T, N>
accepts two required type parameters, the T
must be a tuple
, and the N
must be an integer >=1
type exp1 = Chunk<[1, 2, 3], 2> // expected to be [[1, 2], [3]] type exp2 = Chunk<[1, 2, 3], 4> // expected to be [[1, 2, 3]] type exp3 = Chunk<[1, 2, 3], 1> // expected to be [[1], [2], [3]]
/* _____________ Your Code Here _____________ */ type Chunk<T extends any[], U extends number, ACC extends any[]= []> = ACC['length'] extends U ? [ACC, ...Chunk<T, U>] : T extends [infer F, ...infer RT] ? Chunk<RT, U, [...ACC, F]> : ACC['length'] extends 0 ? [] : [ACC]; /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<Chunk<[], 1>, []>>, Expect<Equal<Chunk<[1, 2, 3], 1>, [[1], [2], [3]]>>, Expect<Equal<Chunk<[1, 2, 3], 2>, [[1, 2], [3]]>>, Expect<Equal<Chunk<[1, 2, 3, 4], 2>, [[1, 2], [3, 4]]>>, Expect<Equal<Chunk<[1, 2, 3, 4], 5>, [[1, 2, 3, 4]]>>, Expect<Equal<Chunk<[1, true, 2, false], 2>, [[1, true], [2, false]]>>, ]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-10-25 [Functional Programming] Use the Callback and Closure Pattern to Build Advanced Async Behaviors [Full code]
2020-10-25 [Javascript] Broadcaster + Operator + Listener pattern -- 11. Customize the done logic
2020-10-25 [Javascript] Broadcaster + Operator + Listener pattern -- 10. Define a Function to Set Common Behaviors in Operators
2020-10-25 [Functional Programming] Start With the API You Want Then Implement
2020-10-25 [Javascript] Broadcaster + Operator + Listener pattern -- 9. Create modify, filter, map operators
2019-10-25 [Schematics] 1. Copy and Manipulate Template
2019-10-25 [Schematics] 0. Schematics "Hello World"