Skip to content

Commit 1e67d71

Browse files
author
chengyifan
committed
merge
1 parent b38bf15 commit 1e67d71

File tree

3 files changed

+238
-115
lines changed

3 files changed

+238
-115
lines changed

collection.ts

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
type _Dd = "0123456789";
2+
type _Al = "abcdefghijklmnopqrstvuwxyz";
3+
type _Au = "ABCDEFGHIJKLMNOPQRSTVUWXYZ";
4+
type _Aa = `${_Al}${_Au}`;
5+
type $Digital = $Split<_Dd>[number];
6+
type $Alphabets_lower = $Split<_Al>[number];
7+
type $Alphabets_upper = $Split<_Au>[number];
8+
type $Alphabets = $Alphabets_lower | $Alphabets_upper;
9+
type $Words = $Digital | $Alphabets | "_";
10+
11+
type $FindCharIndex<
12+
Target extends $Alphabets,
13+
Str extends string = _Aa,
14+
Result extends number = 0
15+
> = Str extends ""
16+
? -1
17+
: Str extends `${infer U}${infer Tail}`
18+
? U extends Target
19+
? Result
20+
: $FindCharIndex<Target, Tail, $Add<Result, 1>["value"]>
21+
: Result;
22+
23+
type $FindDigitalIndex<
24+
Target extends $Digital,
25+
Str extends string = _Dd,
26+
Result extends number = 0
27+
> = Str extends ""
28+
? -1
29+
: Str extends `${infer U}${infer Tail}`
30+
? U extends Target
31+
? Result
32+
: $FindDigitalIndex<Target, Tail, $Add<Result, 1>["value"]>
33+
: Result;
34+
35+
type $Split<Str extends string, Result extends string[] = []> = Str extends ""
36+
? Result
37+
: Str extends `${infer Head}${infer Tail}`
38+
? $Split<Tail, [...Result, Head]>
39+
: [];
40+
41+
type $Slice<
42+
Str extends string,
43+
From extends number,
44+
To extends number,
45+
I extends number = 0,
46+
Arr extends string[] = $Split<Str>,
47+
ToBound extends number = $Min<Arr["length"], To>,
48+
Result extends [string, string, string] = ["", "", ""]
49+
> = $LT<I, From> extends 1
50+
? $Slice<
51+
Str,
52+
From,
53+
To,
54+
$Add<I, 1>["value"],
55+
Arr,
56+
ToBound,
57+
[`${Result[0]}${Arr[I]}`, Result[1], Result[2]]
58+
>
59+
: $LT<I, ToBound> extends 1
60+
? $Slice<
61+
Str,
62+
From,
63+
To,
64+
$Add<I, 1>["value"],
65+
Arr,
66+
ToBound,
67+
[Result[0], `${Result[1]}${Arr[I]}`, Result[2]]
68+
>
69+
: $LT<I, Arr["length"]> extends 1
70+
? $Slice<
71+
Str,
72+
From,
73+
To,
74+
$Add<I, 1>["value"],
75+
Arr,
76+
ToBound,
77+
[Result[0], Result[1], `${Result[2]}${Arr[I]}`]
78+
>
79+
: Result;
80+
81+
type $SliceToCharArray<
82+
Str extends string,
83+
From extends number,
84+
To extends number,
85+
I extends number = 0,
86+
Arr extends string[] = $Split<Str>,
87+
ToBound extends number = $Min<Arr["length"], To>,
88+
Result extends string[] = []
89+
> = $LT<I, From> extends 1
90+
? $SliceToCharArray<Str, From, To, $Add<I, 1>["value"], Arr, ToBound, Result>
91+
: $LT<I, ToBound> extends 1
92+
? $SliceToCharArray<
93+
Str,
94+
From,
95+
To,
96+
$Add<I, 1>["value"],
97+
Arr,
98+
ToBound,
99+
[...Result, Arr[I]]
100+
>
101+
: $LT<I, Arr["length"]> extends 1
102+
? $SliceToCharArray<Str, From, To, $Add<I, 1>["value"], Arr, ToBound, Result>
103+
: Result;
104+
105+
type $Repeat<
106+
Str extends string,
107+
N extends number,
108+
Result extends string = ""
109+
> = N extends 0
110+
? Result
111+
: $Repeat<Str, $Subtract<N, 1>["value"], `${Result}${Str}`>;
112+
113+
type _RepeatRange<
114+
Str extends string,
115+
M extends number,
116+
N extends number,
117+
Result extends string[] = []
118+
> = $LT<M, $Add<N, 1>["value"]> extends 1
119+
? _RepeatRange<Str, $Add<M, 1>["value"], N, [...Result, $Repeat<Str, M>]>
120+
: Result;
121+
122+
type $RepeatRange<
123+
Str extends string,
124+
M extends number,
125+
N extends number
126+
> = $ArrayToUnion<_RepeatRange<Str, M, N>>;
127+
128+
type $ArrayToUnion<T extends any[]> = number extends keyof T
129+
? T[number]
130+
: never;
131+
132+
type $AlphaRange<
133+
From extends $Alphabets,
134+
To extends $Alphabets,
135+
From1 extends number = $FindCharIndex<From>,
136+
To1 extends number = $FindCharIndex<To>
137+
> = $ArrayToUnion<$SliceToCharArray<_Aa, From1, To1>> | To;
138+
139+
type $DigitalRange<
140+
From extends $Digital,
141+
To extends $Digital,
142+
From1 extends number = $FindDigitalIndex<From>,
143+
To1 extends number = $FindDigitalIndex<To>
144+
> = $ArrayToUnion<$SliceToCharArray<_Dd, From1, To1>> | To;
145+
146+
type $ArrayExclude<
147+
Arr extends T[],
148+
P extends T,
149+
T = string,
150+
Result extends T[] = []
151+
> = Arr extends []
152+
? Result
153+
: Arr extends [infer Head, ...infer Tail]
154+
? Head extends T
155+
? Head extends P
156+
? [...Result, ...Tail]
157+
: Tail extends T[]
158+
? $ArrayExclude<Tail, P, T, [...Result, Head]>
159+
: []
160+
: []
161+
: [];
162+
163+
type $Array2InsertHead<
164+
Arr extends T[][],
165+
Insert extends T,
166+
T = string,
167+
Result extends T[][] = []
168+
> = Arr extends []
169+
? Result
170+
: Arr extends [infer Head, ...infer Tail]
171+
? Head extends T[]
172+
? Tail extends T[][]
173+
? $Array2InsertHead<Tail, Insert, T, [...Result, [Insert, ...Head]]>
174+
: []
175+
: []
176+
: [];
177+
178+
type $Arrange<
179+
Coll extends string[],
180+
T extends number = 4,
181+
I extends number = 0,
182+
Result extends string[][] = []
183+
> = T extends 1
184+
? [Coll[0]]
185+
: T extends 2
186+
? [[Coll[1], Coll[0]], [Coll[0], Coll[1]]]
187+
: $LT<I, T> extends 1
188+
? $Arrange<
189+
Coll,
190+
T,
191+
$Add<I, 1>["value"],
192+
[
193+
...Result,
194+
...$Array2InsertHead<
195+
$Arrange<$ArrayExclude<Coll, Coll[I]>, $Subtract<T, 1>["value"]>,
196+
Coll[I]
197+
>
198+
]
199+
>
200+
: Result;
201+
202+
type $Comb2<Coll extends string[]> = [];
203+
204+
// 求子数组(相对顺序不变)
205+
type $Combine<
206+
Coll extends string[],
207+
T extends number = 4,
208+
I extends number = 0,
209+
J extends number = 0,
210+
K extends number = 0,
211+
TempResult extends string[] = [],
212+
Result extends string[][] = []
213+
> = T extends 1
214+
? [[Coll[0]]]
215+
: T extends 2
216+
? [[Coll[0]], [Coll[1]], [Coll[0], Coll[1]]]
217+
: $LT<I, T> extends 1
218+
? $Combine<Coll, T, $Add<I, 1>["value"], I, 0, [], []>
219+
: $LT<J, T> extends 1
220+
? $Combine<Coll, T, I, $Add<J, 1>["value"], I, [], Result>
221+
: Result;
222+
223+
type $MapJoin<
224+
Arr extends string[][],
225+
Sep extends string,
226+
Result extends string[] = []
227+
> = Arr extends []
228+
? Result
229+
: Arr extends [infer Head, ...infer Tail]
230+
? Head extends string[]
231+
? Tail extends string[][]
232+
? $MapJoin<Tail, Sep, [...Result, $Join<Head, Sep>]>
233+
: []
234+
: []
235+
: [];
236+

index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ type R3 = ReduceSum<3>; // 1 + 2 + 3 = 5
5454
type R4 = ReduceSum<4>; // 1 + 2 + 3 + 5 = 10
5555
type R13 = ReduceSum<12>; // 78
5656
type Calc13 = $Divide<$Multiply<$Add<1, 12>, 12>, 2>["value"]; // (x + 1) * x / 2 = 78
57+
58+
// Todo,不支持传递高阶范形,通过指令实现,设计寄存器系统,指令系统,寻址系统

regexp.ts

-115
This file was deleted.

0 commit comments

Comments
 (0)