semantic-typescript 0.0.1 β†’ 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/readme.kr.md ADDED
@@ -0,0 +1,335 @@
1
+ # πŸ“˜ semantic-typescript
2
+
3
+ TypeScriptμ—μ„œ **의미둠적 데이터 처리(Semantic Data Processing)** λ₯Ό μœ„ν•œ κ°•λ ₯ν•˜κ³  νƒ€μž… μ•ˆμ „ν•œ μœ ν‹Έλ¦¬ν‹° λΌμ΄λΈŒλŸ¬λ¦¬μž…λ‹ˆλ‹€.
4
+ μ»¬λ ‰μ…˜, 슀트림, μ‹œν€€μŠ€λ₯Ό ν•¨μˆ˜ν˜• μŠ€νƒ€μΌλ‘œ λ‹€λ£° 수 μžˆλŠ” ꡬ성 μš”μ†Œλ“€μ„ μ œκ³΅ν•˜λ©°, μ •λ ¬, 필터링, κ·Έλ£Ήν™”, 톡계 뢄석 등을 μ§€μ›ν•©λ‹ˆλ‹€.
5
+
6
+ **μ •λ ¬λœ 데이터든 μ •λ ¬λ˜μ§€ μ•Šμ€ 데이터든**, **톡계 뢄석을 ν•˜λ“ **, ν˜Ήμ€ λ‹¨μˆœνžˆ **연산을 μœ μ°½ν•˜κ²Œ μ—°κ²°(chain)ν•˜κ³  μ‹Άλ“ **, 이 λΌμ΄λΈŒλŸ¬λ¦¬κ°€ λ„μ™€λ“œλ¦½λ‹ˆλ‹€.
7
+
8
+ ---
9
+
10
+ ## 🧩 μ£Όμš” κΈ°λŠ₯
11
+
12
+ - βœ… μ „μ²΄μ μœΌλ‘œ **νƒ€μž… μ•ˆμ „ν•œ μ œλ„€λ¦­(Type-safe Generics)**
13
+ - βœ… **ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° μŠ€νƒ€μΌ** (map, filter, reduce λ“±)
14
+ - βœ… **의미둠적 데이터 슀트림(Semantic<E>)** - μ§€μ—° 평가(Lazy Evaluation) 지원
15
+ - βœ… μŠ€νŠΈλ¦Όμ„ κ΅¬μ‘°ν™”λœ λ°μ΄ν„°λ‘œ λ³€ν™˜ν•˜λŠ” **컬렉터(Collectors)**
16
+ - βœ… **μ •λ ¬λœ(Sorted) 및 λΉ„μ •λ ¬λœ(Unordered) 컬렉터** β€” `toUnordered()`λŠ” **κ°€μž₯ λΉ λ₯΄λ©° μ •λ ¬ν•˜μ§€ μ•ŠμŒ**
17
+ - βœ… `sorted()`, `toOrdered()`, λΉ„κ΅μž(Comparator)λ₯Ό ν†΅ν•œ **μ •λ ¬ κΈ°λŠ₯**
18
+ - βœ… **톡계 뢄석(Statistics)** (`NumericStatistics`, `BigIntStatistics` λ“±)
19
+ - βœ… **Optional<T>** β€” null λ˜λŠ” undefined 값을 μ•ˆμ „ν•˜κ²Œ λ‹€λ£¨λŠ” λͺ¨λ‚˜λ“œ(Monad)
20
+ - βœ… **반볡자(Iterator) & 생성기(Generator)** 기반 섀계 β€” λŒ€κ·œλͺ¨ λ˜λŠ” 비동기 데이터에도 적합
21
+
22
+ ---
23
+
24
+ ## πŸ“¦ μ„€μΉ˜
25
+
26
+ ```bash
27
+ npm install semantic-typescript
28
+ ```
29
+
30
+ ---
31
+
32
+ ## 🧠 핡심 κ°œλ…
33
+
34
+ ### 1. `Optional<T>` β€” null/undefined κ°’ μ•ˆμ „ν•˜κ²Œ 처리
35
+
36
+ 값이 null λ˜λŠ” undefined 일 수 μžˆλŠ” 경우λ₯Ό μ•ˆμ „ν•˜κ²Œ μ²˜λ¦¬ν•  수 μžˆλŠ” λͺ¨λ‚˜λ”•(Monadic) μ»¨ν…Œμ΄λ„ˆμž…λ‹ˆλ‹€.
37
+
38
+ #### λ©”μ„œλ“œ:
39
+
40
+ | λ©”μ„œλ“œ | μ„€λͺ… | 예제 |
41
+ |--------|------|------|
42
+ | `of(value)` | κ°’ 감싸기 (null κ°€λŠ₯) | `Optional.of(null)` |
43
+ | `ofNullable(v)` | null ν—ˆμš©ν•˜μ—¬ 감싸기 | `Optional.ofNullable(someVar)` |
44
+ | `ofNonNull(v)` | null이면 μ˜ˆμ™Έ λ°œμƒ | `Optional.ofNonNull(5)` |
45
+ | `get()` | κ°’ κ°€μ Έμ˜€κΈ° (μ—†μœΌλ©΄ μ˜ˆμ™Έ) | `opt.get()` |
46
+ | `getOrDefault(d)` | κ°’ κ°€μ Έμ˜€κΈ° λ˜λŠ” κΈ°λ³Έκ°’ | `opt.getOrDefault(0)` |
47
+ | `ifPresent(fn)` | 값이 있으면 λΆ€μž‘μš© μ‹€ν–‰ | `opt.ifPresent(x => console.log(x))` |
48
+ | `map(fn)` | 값이 있으면 λ³€ν™˜ | `opt.map(x => x + 1)` |
49
+ | `filter(fn)` | 쑰건을 λ§Œμ‘±ν•˜λŠ” κ°’λ§Œ μœ μ§€ | `opt.filter(x => x > 0)` |
50
+ | `isEmpty()` | λΉ„μ–΄μžˆλŠ”μ§€ 확인 | `opt.isEmpty()` |
51
+ | `isPresent()` | 값이 μžˆλŠ”μ§€ 확인 | `opt.isPresent()` |
52
+
53
+ #### 예제:
54
+
55
+ ```typescript
56
+ import { Optional } from 'semantic-typescript';
57
+
58
+ const value: number | null = Math.random() > 0.5 ? 10 : null;
59
+
60
+ const opt = Optional.ofNullable(value);
61
+
62
+ const result = opt
63
+ .filter(v => v > 5)
64
+ .map(v => v * 2)
65
+ .getOrDefault(0);
66
+
67
+ console.log(result); // 20 λ˜λŠ” 0
68
+ ```
69
+
70
+ ---
71
+
72
+ ### 2. `Semantic<E>` β€” μ§€μ—° 평가 데이터 슀트림
73
+
74
+ μš”μ†Œλ“€μ˜ **μ§€μ—° 평가(Lazy), ꡬ성 κ°€λŠ₯ν•œ μ‹œν€€μŠ€**μž…λ‹ˆλ‹€. Java의 Streamsλ‚˜ Kotlin의 Sequences와 μœ μ‚¬ν•©λ‹ˆλ‹€.
75
+
76
+ `from()`, `range()`, `iterate()`, `fill()` 같은 헬퍼 ν•¨μˆ˜λ‘œ `Semantic` μŠ€νŠΈλ¦Όμ„ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.
77
+
78
+ #### 생성 방법:
79
+
80
+ | ν•¨μˆ˜ | μ„€λͺ… | 예제 |
81
+ |------|------|------|
82
+ | `from(iterable)` | λ°°μ—΄, Set, 반볡 κ°€λŠ₯ν•œ κ°μ²΄λ‘œλΆ€ν„° 생성 | `from([1, 2, 3])` |
83
+ | `range(start, end, step?)` | 숫자 λ²”μœ„ 생성 | `range(0, 5)` β†’ 0,1,2,3,4 |
84
+ | `fill(element, count)` | μš”μ†Œλ₯Ό N번 반볡 | `fill('a', 3n)` |
85
+ | `iterate(gen)` | μ‚¬μš©μž μ •μ˜ 생성기 ν•¨μˆ˜ μ‚¬μš© | `iterate(genFn)` |
86
+
87
+ #### μ£Όμš” μ—°μ‚°μž:
88
+
89
+ | λ©”μ„œλ“œ | μ„€λͺ… | 예제 |
90
+ |--------|------|------|
91
+ | `map(fn)` | 각 μš”μ†Œ λ³€ν™˜ | `.map(x => x * 2)` |
92
+ | `filter(fn)` | 쑰건을 λ§Œμ‘±ν•˜λŠ” μš”μ†Œλ§Œ μœ μ§€ | `.filter(x => x > 10)` |
93
+ | `limit(n)` | 처음 N개 μš”μ†Œλ‘œ μ œν•œ | `.limit(5)` |
94
+ | `skip(n)` | 처음 N개 μš”μ†Œ κ±΄λ„ˆλ›°κΈ° | `.skip(2)` |
95
+ | `distinct()` | 쀑볡 제거 (기본적으둜 Set μ‚¬μš©) | `.distinct()` |
96
+ | `sorted()` | μš”μ†Œ μ •λ ¬ (μžμ—° μˆœμ„œ) | `.sorted()` |
97
+ | `sorted(comparator)` | μ»€μŠ€ν…€ μ •λ ¬ | `.sorted((a, b) => a - b)` |
98
+ | `toOrdered()` | μ •λ ¬ ν›„ `OrderedCollectable` λ°˜ν™˜ | `.toOrdered()` |
99
+ | `toUnordered()` | **μ •λ ¬ μ—†μŒ – κ°€μž₯ 빠름** | `.toUnordered()` βœ… |
100
+ | `collect(collector)` | μ»¬λ ‰ν„°λ‘œ 집계 | `.collect(Collector.full(...))` |
101
+ | `toArray()` | λ°°μ—΄λ‘œ λ³€ν™˜ | `.toArray()` |
102
+ | `toSet()` | Set으둜 λ³€ν™˜ | `.toSet()` |
103
+ | `toMap(keyFn, valFn)` | Map으둜 λ³€ν™˜ | `.toMap(x => x.id, x => x)` |
104
+
105
+ ---
106
+
107
+ ### 3. `toUnordered()` β€” πŸš€ κ°€μž₯ 빠름, μ •λ ¬ μ—†μŒ
108
+
109
+ **정렬이 ν•„μš” μ—†κ³  졜고의 μ„±λŠ₯을 μ›ν•œλ‹€λ©΄** μ•„λž˜μ™€ 같이 μ‚¬μš©ν•˜μ„Έμš”:
110
+
111
+ ```typescript
112
+ const fastest = semanticStream.toUnordered();
113
+ ```
114
+
115
+ πŸ”₯ **μ–΄λ– ν•œ μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜λ„ μ μš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.**
116
+ μˆœμ„œκ°€ μ€‘μš”ν•˜μ§€ μ•Šκ³  속도가 μ΅œμš°μ„ μΌ λ•Œ 이 방법을 μ‚¬μš©ν•˜μ„Έμš”.
117
+
118
+ ---
119
+
120
+ ### 4. `toOrdered()` 및 `sorted()` β€” μ •λ ¬λœ 좜λ ₯
121
+
122
+ **μ •λ ¬λœ κ²°κ³Όκ°€ ν•„μš”ν•œ 경우** λ‹€μŒμ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€:
123
+
124
+ ```typescript
125
+ const ordered = semanticStream.sorted(); // μžμ—° μ •λ ¬
126
+ const customSorted = semanticStream.sorted((a, b) => a - b); // μ»€μŠ€ν…€ λΉ„κ΅μž
127
+ const orderedCollectable = semanticStream.toOrdered(); // 정렬됨
128
+ ```
129
+
130
+ ⚠️ 이 λ©”μ„œλ“œλ“€μ€ **μš”μ†Œλ₯Ό μ •λ ¬**ν•˜λ©°, μžμ—° μ •λ ¬ λ˜λŠ” 제곡된 λΉ„κ΅μžλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.
131
+
132
+ ---
133
+
134
+ ### 5. `Collector<E, A, R>` β€” 데이터 집계
135
+
136
+ 컬렉터λ₯Ό μ‚¬μš©ν•˜λ©΄ μŠ€νŠΈλ¦Όμ„ **단일 κ°’ λ˜λŠ” λ³΅μž‘ν•œ ꡬ쑰둜 μΆ•μ•½**ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
137
+
138
+ λ‚΄μž₯된 정적 νŒ©ν† λ¦¬:
139
+
140
+ ```typescript
141
+ Collector.full(identity, accumulator, finisher)
142
+ Collector.shortable(identity, interruptor, accumulator, finisher)
143
+ ```
144
+
145
+ ν•˜μ§€λ§Œ 보톡은 `Collectable` 클래슀의 κ³ μˆ˜μ€€ λ©”μ„œλ“œλ₯Ό 톡해 μ‚¬μš©ν•˜κ²Œ λ©λ‹ˆλ‹€.
146
+
147
+ ---
148
+
149
+ ### 6. `Collectable<E>` (좔상 클래슀)
150
+
151
+ λ‹€μŒ ν΄λž˜μŠ€λ“€μ˜ 기반 ν΄λž˜μŠ€μž…λ‹ˆλ‹€:
152
+
153
+ - `OrderedCollectable<E>` β€” μ •λ ¬λœ 좜λ ₯
154
+ - `UnorderedCollectable<E>` β€” μ •λ ¬ μ—†μŒ, κ°€μž₯ 빠름
155
+ - `WindowCollectable<E>` β€” μŠ¬λΌμ΄λ”© μœˆλ„μš°
156
+ - `Statistics<E, D>` β€” 톡계 집계
157
+
158
+ #### 곡톡 λ©”μ„œλ“œ (상속을 톡해):
159
+
160
+ | λ©”μ„œλ“œ | μ„€λͺ… | 예제 |
161
+ |--------|------|------|
162
+ | `count()` | μš”μ†Œ 개수 | `.count()` |
163
+ | `toArray()` | λ°°μ—΄λ‘œ λ³€ν™˜ | `.toArray()` |
164
+ | `toSet()` | Set으둜 λ³€ν™˜ | `.toSet()` |
165
+ | `toMap(k, v)` | Map으둜 λ³€ν™˜ | `.toMap(x => x.id, x => x)` |
166
+ | `group(k)` | ν‚€λ‘œ κ·Έλ£Ήν™” | `.group(x => x.category)` |
167
+ | `findAny()` | μž„μ˜μ˜ μš”μ†Œ (Optional) | `.findAny()` |
168
+ | `findFirst()` | 첫 번째 μš”μ†Œ (Optional) | `.findFirst()` |
169
+ | `reduce(...)` | μ»€μŠ€ν…€ μΆ•μ•½ | `.reduce((a,b) => a + b, 0)` |
170
+
171
+ ---
172
+
173
+ ### 7. `OrderedCollectable<E>` β€” μ •λ ¬λœ 데이터
174
+
175
+ μš”μ†Œλ“€μ„ **μžλ™μœΌλ‘œ μ •λ ¬**ν•˜κ³  μ‹Άλ‹€λ©΄ 이 클래슀λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
176
+
177
+ **μ»€μŠ€ν…€ λΉ„κ΅μž**λ₯Ό λ°›κ±°λ‚˜ μžμ—° 정렬을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
178
+
179
+ ```typescript
180
+ const sorted = new OrderedCollectable(stream);
181
+ const customSorted = new OrderedCollectable(stream, (a, b) => b - a);
182
+ ```
183
+
184
+ πŸ”’ **μ •λ ¬λœ 좜λ ₯이 보μž₯λ©λ‹ˆλ‹€.**
185
+
186
+ ---
187
+
188
+ ### 8. `UnorderedCollectable<E>` β€” μ •λ ¬ μ—†μŒ (πŸš€ κ°€μž₯ 빠름)
189
+
190
+ **μˆœμ„œκ°€ μ€‘μš”ν•˜μ§€ μ•Šκ³  졜고의 μ„±λŠ₯이 ν•„μš”ν•œ 경우** λ‹€μŒμ„ μ‚¬μš©ν•˜μ„Έμš”:
191
+
192
+ ```typescript
193
+ const unordered = new UnorderedCollectable(stream);
194
+ // λ˜λŠ”
195
+ const fastest = semanticStream.toUnordered();
196
+ ```
197
+
198
+ βœ… **μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ΄ μ‹€ν–‰λ˜μ§€ μ•ŠμŒ**
199
+ βœ… **μˆœμ„œκ°€ μ€‘μš”ν•˜μ§€ μ•Šμ„ λ•Œ 졜고의 μ„±λŠ₯**
200
+
201
+ ---
202
+
203
+ ### 9. `Statistics<E, D>` β€” 톡계 뢄석
204
+
205
+ 숫자 데이터λ₯Ό λΆ„μ„ν•˜κΈ° μœ„ν•œ 좔상 기반 ν΄λž˜μŠ€μž…λ‹ˆλ‹€.
206
+
207
+ #### μ„œλΈŒ 클래슀:
208
+
209
+ - `NumericStatistics<E>` β€” `number` κ°’μš©
210
+ - `BigIntStatistics<E>` β€” `bigint` κ°’μš©
211
+
212
+ ##### μ£Όμš” 톡계 λ©”μ„œλ“œ:
213
+
214
+ | λ©”μ„œλ“œ | μ„€λͺ… | 예제 |
215
+ |--------|------|------|
216
+ | `mean()` | 평균 | `.mean()` |
217
+ | `median()` | 쀑앙값 | `.median()` |
218
+ | `mode()` | μ΅œλΉˆκ°’ | `.mode()` |
219
+ | `minimum()` | μ΅œμ†Ÿκ°’ | `.minimum()` |
220
+ | `maximum()` | μ΅œλŒ“κ°’ | `.maximum()` |
221
+ | `range()` | μ΅œλŒ“κ°’ - μ΅œμ†Ÿκ°’ | `.range()` |
222
+ | `variance()` | λΆ„μ‚° | `.variance()` |
223
+ | `standardDeviation()` | ν‘œμ€€ 편차 | `.standardDeviation()` |
224
+ | `summate()` | 합계 | `.summate()` |
225
+ | `quantile(q)` | qλΆ„μœ„μˆ˜ (0~1) | `.quantile(0.5)` β†’ 쀑앙값 |
226
+ | `frequency()` | λΉˆλ„ λ§΅ | `.frequency()` |
227
+
228
+ ---
229
+
230
+ ## πŸ§ͺ 전체 예제
231
+
232
+ ```typescript
233
+ import { from, toUnordered, toOrdered, sorted, NumericStatistics } from 'semantic-typescript';
234
+
235
+ // μƒ˜ν”Œ 데이터
236
+ const numbers = from([10, 2, 8, 4, 5, 6]);
237
+
238
+ // πŸš€ κ°€μž₯ 빠름: μ •λ ¬ μ—†μŒ
239
+ const fastest = numbers.toUnordered();
240
+ console.log(fastest.toArray()); // 예: [10, 2, 8, 4, 5, 6] (μ›λž˜ μˆœμ„œ)
241
+
242
+ // πŸ”’ μžμ—° μ •λ ¬
243
+ const ordered = numbers.sorted();
244
+ console.log(ordered.toArray()); // [2, 4, 5, 6, 8, 10]
245
+
246
+ // πŸ“Š 톡계 뢄석
247
+ const stats = new NumericStatistics(numbers);
248
+ console.log('평균:', stats.mean());
249
+ console.log('쀑앙값:', stats.median());
250
+ console.log('μ΅œλΉˆκ°’:', stats.mode());
251
+ console.log('λ²”μœ„:', stats.range());
252
+ console.log('ν‘œμ€€ 편차:', stats.standardDeviation());
253
+ ```
254
+
255
+ ---
256
+
257
+ ## πŸ› οΈ μœ ν‹Έλ¦¬ν‹° ν•¨μˆ˜
258
+
259
+ λ‹€μ–‘ν•œ **νƒ€μž… κ°€λ“œ(Type Guards)** 와 **비ꡐ 도ꡬ**도 μ œκ³΅λ©λ‹ˆλ‹€:
260
+
261
+ | ν•¨μˆ˜ | μš©λ„ |
262
+ |------|------|
263
+ | `isString(x)` | `string` νƒ€μž… κ°€λ“œ |
264
+ | `isNumber(x)` | `number` νƒ€μž… κ°€λ“œ |
265
+ | `isBoolean(x)` | `boolean` νƒ€μž… κ°€λ“œ |
266
+ | `isIterable(x)` | 객체가 반볡 κ°€λŠ₯ν•œμ§€ 확인 |
267
+ | `useCompare(a, b)` | λ²”μš© 비ꡐ ν•¨μˆ˜ |
268
+ | `useRandom(x)` | μ˜μ‚¬ λ‚œμˆ˜ 생성기 (μž¬λ―Έμ‚Όμ•„) |
269
+
270
+ ---
271
+
272
+ ## 🧩 κ³ κΈ‰: μ»€μŠ€ν…€ 생성기 & μœˆλ„μš°
273
+
274
+ λ¬΄ν•œ λ˜λŠ” μ œμ–΄λœ 데이터 μŠ€νŠΈλ¦Όμ„ μœ„ν•œ **μ»€μŠ€ν…€ 생성기**λ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€:
275
+
276
+ ```typescript
277
+ const gen = (accept: BiConsumer<number, bigint>, interrupt: Predicate<number>) => {
278
+ for (let i = 0; i < 10; i++) {
279
+ accept(i, BigInt(i));
280
+ if (i === 5) interrupt(i);
281
+ }
282
+ };
283
+
284
+ const s = new Semantic(gen);
285
+ ```
286
+
287
+ λ˜λŠ” **μŠ¬λΌμ΄λ”© μœˆλ„μš°**λ₯Ό μ‚¬μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€:
288
+
289
+ ```typescript
290
+ const windowed = ordered.slide(3n, 2n); // 크기 3, μŠ€ν… 2
291
+ ```
292
+
293
+ ---
294
+
295
+ ## πŸ“„ λΌμ΄μ„ μŠ€
296
+
297
+ 이 ν”„λ‘œμ νŠΈλŠ” **MIT λΌμ΄μ„ μŠ€** 둜 제곡되며, μƒμ—…μ Β·κ°œμΈμ  μš©λ„λ‘œ 자유둭게 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
298
+
299
+ ---
300
+
301
+ ## πŸ™Œ κΈ°μ—¬
302
+
303
+ Pull Request, 이슈(Issue), 아이디어 μ œμ•ˆ λ“± λͺ¨λ‘ ν™˜μ˜ν•©λ‹ˆλ‹€!
304
+
305
+ ---
306
+
307
+ ## πŸš€ λΉ λ₯Έ μ‹œμž‘ μš”μ•½
308
+
309
+ | μž‘μ—… | λ©”μ„œλ“œ |
310
+ |------|--------|
311
+ | null/undefined μ•ˆμ „ν•˜κ²Œ 처리 | `Optional<T>` |
312
+ | 슀트림 생성 | `from([...])`, `range()`, `fill()` |
313
+ | 데이터 λ³€ν™˜ | `map()`, `filter()` |
314
+ | 데이터 μ •λ ¬ | `sorted()`, `toOrdered()` |
315
+ | μ •λ ¬ μ—†μŒ (κ°€μž₯ 빠름) | `toUnordered()` βœ… |
316
+ | κ·Έλ£Ήν™” / 집계 | `toMap()`, `group()`, `Collector` |
317
+ | 톡계 | `NumericStatistics`, `mean()`, `median()` λ“± |
318
+
319
+ ---
320
+
321
+ ## πŸ”— 링크
322
+
323
+ - πŸ“¦ npm: https://www.npmjs.com/package/semantic-typescript
324
+ - πŸ™ GitHub: https://github.com/eloyhere/semantic-typescript
325
+ - πŸ“˜ λ¬Έμ„œ: μ†ŒμŠ€ μ½”λ“œ / νƒ€μž… μ •μ˜ μ°Έμ‘°
326
+
327
+ ---
328
+
329
+ **TypeScriptμ—μ„œ ν•¨μˆ˜ν˜•, νƒ€μž… μ•ˆμ „ν•˜κ³  ꡬ성 κ°€λŠ₯ν•œ 데이터 처리λ₯Ό μ¦κ²¨λ³΄μ„Έμš”.** πŸš€
330
+
331
+ ---
332
+
333
+ βœ… **μ°Έκ³ :**
334
+ - `toUnordered()` β†’ **μ •λ ¬ μ—†μŒ, κ°€μž₯ 빠름**
335
+ - κ·Έ μ™Έ (`sorted()`, `toOrdered()` λ“±) β†’ **데이터λ₯Ό 정렬함**
package/readme.md ADDED
@@ -0,0 +1,335 @@
1
+ # πŸ“˜ semantic-typescript
2
+
3
+ A powerful, type-safe utility library for **semantic data processing** in TypeScript.
4
+ Provides composable, functional-style constructs for working with collections, streams, and sequences β€” with support for sorting, filtering, grouping, statistics, and more.
5
+
6
+ Whether you're processing **ordered or unordered data**, performing **statistical analysis**, or simply **chaining operations fluently**, this library has you covered.
7
+
8
+ ---
9
+
10
+ ## 🧩 Features
11
+
12
+ - βœ… **Type-safe generics** throughout
13
+ - βœ… **Functional programming** style (map, filter, reduce, etc.)
14
+ - βœ… **Semantic data streams** (`Semantic<E>`) for lazy evaluation
15
+ - βœ… **Collectors** for transforming streams into concrete structures
16
+ - βœ… **Ordered & Unordered Collectables** β€” with `toUnordered()` being the **fastest** (no sorting)
17
+ - βœ… **Sorting support** via `sorted()`, `toOrdered()`, comparators
18
+ - βœ… **Statistical analysis** (`Statistics`, `NumericStatistics`, `BigIntStatistics`)
19
+ - βœ… **Optional<T>** monad for safe nullable handling
20
+ - βœ… **Iterators & Generators** based design β€” suitable for large or asynchronous data
21
+
22
+ ---
23
+
24
+ ## πŸ“¦ Installation
25
+
26
+ ```bash
27
+ npm install semantic-typescript
28
+ ```
29
+
30
+ ---
31
+
32
+ ## 🧠 Core Concepts
33
+
34
+ ### 1. `Optional<T>` – Safe Nullable Handling
35
+
36
+ A monadic container for values that may be `null` or `undefined`.
37
+
38
+ #### Methods:
39
+
40
+ | Method | Description | Example |
41
+ |----------------|--------------------------------------------------|---------|
42
+ | `of(value)` | Wrap a value (may be nullish) | `Optional.of(null)` |
43
+ | `ofNullable(v)`| Wrap, allowing nullish values | `Optional.ofNullable(someVar)` |
44
+ | `ofNonNull(v)` | Wrap, throws if null/undefined | `Optional.ofNonNull(5)` |
45
+ | `get()` | Retrieve value or throw if empty | `opt.get()` |
46
+ | `getOrDefault(d)`| Retrieve value or default | `opt.getOrDefault(0)` |
47
+ | `ifPresent(fn)`| Execute side-effect if present | `opt.ifPresent(x => console.log(x))` |
48
+ | `map(fn)` | Transform value if present | `opt.map(x => x + 1)` |
49
+ | `filter(fn)` | Retain value only if predicate passes | `opt.filter(x => x > 0)` |
50
+ | `isEmpty()` | Check if empty | `opt.isEmpty()` |
51
+ | `isPresent()` | Check if contains a value | `opt.isPresent()` |
52
+
53
+ #### Example:
54
+
55
+ ```typescript
56
+ import { Optional } from 'semantic-typescript';
57
+
58
+ const value: number | null = Math.random() > 0.5 ? 10 : null;
59
+
60
+ const opt = Optional.ofNullable(value);
61
+
62
+ const result = opt
63
+ .filter(v => v > 5)
64
+ .map(v => v * 2)
65
+ .getOrDefault(0);
66
+
67
+ console.log(result); // 20 or 0
68
+ ```
69
+
70
+ ---
71
+
72
+ ### 2. `Semantic<E>` – Lazy Data Stream
73
+
74
+ A **lazy, composable sequence** of elements. Resembles functional streams such as Java Streams or Kotlin Sequences.
75
+
76
+ Create a `Semantic` using helpers like `from()`, `range()`, `iterate()`, or `fill()`.
77
+
78
+ #### Creators:
79
+
80
+ | Function | Description | Example |
81
+ |----------------|----------------------------------------------|---------|
82
+ | `from(iterable)` | Create from Array/Set/Iterable | `from([1, 2, 3])` |
83
+ | `range(start, end, step?)` | Generate number range | `range(0, 5)` β†’ 0,1,2,3,4 |
84
+ | `fill(element, count)` | Repeat an element N times | `fill('a', 3n)` |
85
+ | `iterate(gen)` | Use a custom generator function | `iterate(genFn)` |
86
+
87
+ #### Common Operators:
88
+
89
+ | Method | Description | Example |
90
+ |--------------------|---------------------------------------------------|---------|
91
+ | `map(fn)` | Transform each element | `.map(x => x * 2)` |
92
+ | `filter(fn)` | Retain elements passing predicate | `.filter(x => x > 10)` |
93
+ | `limit(n)` | Limit to first N elements | `.limit(5)` |
94
+ | `skip(n)` | Skip first N elements | `.skip(2)` |
95
+ | `distinct()` | Remove duplicates (uses Set by default) | `.distinct()` |
96
+ | `sorted()` | Sort elements (natural ordering) | `.sorted()` |
97
+ | `sorted(comparator)`| Custom sorting | `.sorted((a, b) => a - b)` |
98
+ | `toOrdered()` | Sort and return `OrderedCollectable` | `.toOrdered()` |
99
+ | `toUnordered()` | **No sorting** – fastest possible collectable | `.toUnordered()` βœ… |
100
+ | `collect(collector)`| Aggregate using a `Collector` | `.collect(Collector.full(...))` |
101
+ | `toArray()` | Convert to Array | `.toArray()` |
102
+ | `toSet()` | Convert to Set | `.toSet()` |
103
+ | `toMap(keyFn, valFn)`| Convert to Map | `.toMap(x => x.id, x => x)` |
104
+
105
+ ---
106
+
107
+ ### 3. `toUnordered()` – πŸš€ Fastest, No Sorting
108
+
109
+ If you **do not require ordering** and seek the **fastest possible performance**, use:
110
+
111
+ ```typescript
112
+ const fastest = semanticStream.toUnordered();
113
+ ```
114
+
115
+ πŸ”₯ **No sorting algorithm is applied.**
116
+ Ideal when order is irrelevant and maximum speed is desired.
117
+
118
+ ---
119
+
120
+ ### 4. `toOrdered()` and `sorted()` – Sorted Output
121
+
122
+ If you need **sorted output**, use:
123
+
124
+ ```typescript
125
+ const ordered = semanticStream.sorted(); // Natural ordering
126
+ const customSorted = semanticStream.sorted((a, b) => a - b); // Custom comparator
127
+ const orderedCollectable = semanticStream.toOrdered(); // Also sorted
128
+ ```
129
+
130
+ ⚠️ These methods **will sort** the elements using either natural or provided comparator.
131
+
132
+ ---
133
+
134
+ ### 5. `Collector<E, A, R>` – Aggregate Data
135
+
136
+ Collectors enable you to **reduce streams into single or complex structures**.
137
+
138
+ Built-in static factories:
139
+
140
+ ```typescript
141
+ Collector.full(identity, accumulator, finisher)
142
+ Collector.shortable(identity, interruptor, accumulator, finisher)
143
+ ```
144
+
145
+ Typically used via higher-level helpers on `Collectable` classes.
146
+
147
+ ---
148
+
149
+ ### 6. `Collectable<E>` (Abstract)
150
+
151
+ Base class for:
152
+
153
+ - `OrderedCollectable<E>` – Sorted output
154
+ - `UnorderedCollectable<E>` – No sorting, fastest
155
+ - `WindowCollectable<E>` – Sliding windows
156
+ - `Statistics<E, D>` – Statistical aggregations
157
+
158
+ #### Common Methods (via inheritance):
159
+
160
+ | Method | Description | Example |
161
+ |----------------|------------------------------------------|---------|
162
+ | `count()` | Count elements | `.count()` |
163
+ | `toArray()` | Convert to Array | `.toArray()` |
164
+ | `toSet()` | Convert to Set | `.toSet()` |
165
+ | `toMap(k, v)` | Convert to Map | `.toMap(x => x.id, x => x)` |
166
+ | `group(k)` | Group by key | `.group(x => x.category)` |
167
+ | `findAny()` | Any matching element (Optional) | `.findAny()` |
168
+ | `findFirst()` | First element (Optional) | `.findFirst()` |
169
+ | `reduce(...)` | Custom reduction | `.reduce((a,b) => a + b, 0)` |
170
+
171
+ ---
172
+
173
+ ### 7. `OrderedCollectable<E>` – Sorted Data
174
+
175
+ If you require elements to be **automatically sorted**, use this.
176
+
177
+ Accepts a **custom comparator** or uses natural order.
178
+
179
+ ```typescript
180
+ const sorted = new OrderedCollectable(stream);
181
+ const customSorted = new OrderedCollectable(stream, (a, b) => b - a);
182
+ ```
183
+
184
+ πŸ”’ **Sorted output is guaranteed.**
185
+
186
+ ---
187
+
188
+ ### 8. `UnorderedCollectable<E>` – No Sorting (πŸš€ Fastest)
189
+
190
+ If you **do not require ordering** and seek the **fastest performance**, use:
191
+
192
+ ```typescript
193
+ const unordered = new UnorderedCollectable(stream);
194
+ // OR
195
+ const fastest = semanticStream.toUnordered();
196
+ ```
197
+
198
+ βœ… **No sorting algorithm executed**
199
+ βœ… **Best performance** when order is irrelevant
200
+
201
+ ---
202
+
203
+ ### 9. `Statistics<E, D>` – Statistical Analysis
204
+
205
+ Abstract base for analysing numeric data.
206
+
207
+ #### Subclasses:
208
+
209
+ - `NumericStatistics<E>` – For `number` values
210
+ - `BigIntStatistics<E>` – For `bigint` values
211
+
212
+ ##### Common statistical methods:
213
+
214
+ | Method | Description | Example |
215
+ |-------------------|--------------------------------------|---------|
216
+ | `mean()` | Arithmetic mean | `.mean()` |
217
+ | `median()` | Median value | `.median()` |
218
+ | `mode()` | Most frequent value | `.mode()` |
219
+ | `minimum()` | Smallest element | `.minimum()` |
220
+ | `maximum()` | Largest element | `.maximum()` |
221
+ | `range()` | Maximum βˆ’ Minimum | `.range()` |
222
+ | `variance()` | Variance | `.variance()` |
223
+ | `standardDeviation()` | Standard deviation | `.standardDeviation()` |
224
+ | `summate()` | Sum of elements | `.summate()` |
225
+ | `quantile(q)` | Value at q-th percentile (0–1) | `.quantile(0.5)` β†’ median |
226
+ | `frequency()` | Frequency map | `.frequency()` |
227
+
228
+ ---
229
+
230
+ ## πŸ§ͺ Full Example
231
+
232
+ ```typescript
233
+ import { from, toUnordered, toOrdered, sorted, NumericStatistics } from 'semantic-typescript';
234
+
235
+ // Sample data
236
+ const numbers = from([10, 2, 8, 4, 5, 6]);
237
+
238
+ // πŸš€ Fastest: no sorting
239
+ const fastest = numbers.toUnordered();
240
+ console.log(fastest.toArray()); // e.g. [10, 2, 8, 4, 5, 6] (original order)
241
+
242
+ // πŸ”’ Naturally sorted
243
+ const ordered = numbers.sorted();
244
+ console.log(ordered.toArray()); // [2, 4, 5, 6, 8, 10]
245
+
246
+ // πŸ“Š Perform statistical analysis
247
+ const stats = new NumericStatistics(numbers);
248
+ console.log('Mean:', stats.mean());
249
+ console.log('Median:', stats.median());
250
+ console.log('Mode:', stats.mode());
251
+ console.log('Range:', stats.range());
252
+ console.log('Standard Deviation:', stats.standardDeviation());
253
+ ```
254
+
255
+ ---
256
+
257
+ ## πŸ› οΈ Utility Functions
258
+
259
+ The library also exports numerous **type guards** and **comparison utilities**:
260
+
261
+ | Function | Purpose |
262
+ |------------------|----------------------------------|
263
+ | `isString(x)` | Type guard for `string` |
264
+ | `isNumber(x)` | Type guard for `number` |
265
+ | `isBoolean(x)` | Type guard for `boolean` |
266
+ | `isIterable(x)` | Checks if object is iterable |
267
+ | `useCompare(a, b)`| Universal comparison function |
268
+ | `useRandom(x)` | Pseudo-random generator (fun) |
269
+
270
+ ---
271
+
272
+ ## 🧩 Advanced: Custom Generators & Windows
273
+
274
+ You may create custom **generators** for infinite or controlled data streams:
275
+
276
+ ```typescript
277
+ const gen = (accept: BiConsumer<number, bigint>, interrupt: Predicate<number>) => {
278
+ for (let i = 0; i < 10; i++) {
279
+ accept(i, BigInt(i));
280
+ if (i === 5) interrupt(i);
281
+ }
282
+ };
283
+
284
+ const s = new Semantic(gen);
285
+ ```
286
+
287
+ Or employ **sliding windows**:
288
+
289
+ ```typescript
290
+ const windowed = ordered.slide(3n, 2n); // windows of 3, stepping by 2
291
+ ```
292
+
293
+ ---
294
+
295
+ ## πŸ“„ License
296
+
297
+ This project is licensed under the **MIT License** – free for commercial and personal use.
298
+
299
+ ---
300
+
301
+ ## πŸ™Œ Contributing
302
+
303
+ Pull requests, issues, and suggestions are most welcome!
304
+
305
+ ---
306
+
307
+ ## πŸš€ Quick Start Summary
308
+
309
+ | Task | Method |
310
+ |------|--------|
311
+ | Safely handle nulls | `Optional<T>` |
312
+ | Create a stream | `from([...])`, `range()`, `fill()` |
313
+ | Transform data | `map()`, `filter()` |
314
+ | Sort data | `sorted()`, `toOrdered()` |
315
+ | No sort (fastest) | `toUnordered()` βœ… |
316
+ | Group / aggregate | `toMap()`, `group()`, `Collector` |
317
+ | Statistics | `NumericStatistics`, `mean()`, `median()`, etc. |
318
+
319
+ ---
320
+
321
+ ## πŸ”— Links
322
+
323
+ - πŸ“¦ npm: https://www.npmjs.com/package/semantic-typescript
324
+ - πŸ™ GitHub: https://github.com/eloyhere/semantic-typescript
325
+ - πŸ“˜ Documentation: See source code / type definitions
326
+
327
+ ---
328
+
329
+ **Enjoy composable, type-safe, functional data processing in TypeScript.** πŸš€
330
+
331
+ ---
332
+
333
+ βœ… **Remember:**
334
+ - `toUnordered()` β†’ **No sort, fastest**
335
+ - All others (e.g. `sorted()`, `toOrdered()`) β†’ **Sort data**