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/package.json +1 -1
- package/readme.de.md +335 -0
- package/readme.es.md +335 -0
- package/readme.fr.md +335 -0
- package/readme.jp.md +335 -0
- package/readme.kr.md +335 -0
- package/readme.md +335 -0
- package/readme.tw.md +335 -0
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**
|