atomic-fns 0.3.5 → 0.4.3
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.md +1 -12
- package/{core → dist}/collections/BTree.d.ts +6 -6
- package/{core → dist}/collections/BTree.js +17 -17
- package/{core → dist}/collections/Heap.d.ts +3 -3
- package/{core → dist}/collections/Heap.js +0 -0
- package/dist/collections/LRUCache.d.ts +80 -0
- package/dist/collections/LRUCache.js +126 -0
- package/{core → dist}/collections/SortedMap.d.ts +2 -2
- package/{core → dist}/collections/SortedMap.js +0 -0
- package/{core → dist}/collections/SortedSet.d.ts +3 -3
- package/{core → dist}/collections/SortedSet.js +1 -1
- package/{core → dist}/collections/SortedTree.d.ts +3 -3
- package/{core → dist}/collections/SortedTree.js +1 -1
- package/{core → dist}/collections/SplayTree.d.ts +5 -5
- package/{core → dist}/collections/SplayTree.js +0 -0
- package/{core → dist}/collections/abc.d.ts +0 -0
- package/{core → dist}/collections/abc.js +0 -0
- package/{core → dist}/collections/deque.d.ts +7 -7
- package/{core → dist}/collections/deque.js +6 -8
- package/{core → dist}/collections/frozenset.d.ts +0 -0
- package/{core → dist}/collections/frozenset.js +3 -6
- package/{core → dist}/collections/index.d.ts +207 -105
- package/{core → dist}/collections/index.js +179 -436
- package/{core → dist}/functools/index.d.ts +30 -16
- package/{core → dist}/functools/index.js +34 -12
- package/dist/globals/decimal.d.ts +142 -0
- package/dist/globals/decimal.js +371 -0
- package/{core → dist}/globals/index.d.ts +26 -25
- package/{core → dist}/globals/index.js +16 -37
- package/dist/i18n/currency.d.ts +24 -0
- package/dist/i18n/currency.js +85 -0
- package/dist/i18n/date/index.d.ts +204 -0
- package/dist/i18n/date/index.js +530 -0
- package/dist/i18n/date/utils.d.ts +65 -0
- package/dist/i18n/date/utils.js +290 -0
- package/dist/i18n/duration.d.ts +112 -0
- package/dist/i18n/duration.js +392 -0
- package/dist/i18n/format.d.ts +26 -0
- package/dist/i18n/format.js +495 -0
- package/{core → dist}/i18n/index.d.ts +2 -1
- package/{core → dist}/i18n/index.js +2 -1
- package/{core → dist}/index.d.ts +0 -0
- package/{core → dist}/index.js +0 -0
- package/{core → dist}/itertools/index.d.ts +74 -46
- package/{core → dist}/itertools/index.js +28 -56
- package/{core → dist}/math/index.d.ts +9 -10
- package/{core → dist}/math/index.js +3 -1
- package/{core → dist}/operators/index.d.ts +4 -4
- package/{core → dist}/operators/index.js +2 -8
- package/{core → dist}/random/index.d.ts +1 -1
- package/{core → dist}/random/index.js +5 -5
- package/package.json +28 -42
- package/.editorconfig +0 -12
- package/.esdoc.json +0 -27
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -35
- package/.firebaserc +0 -5
- package/.prettierrc +0 -19
- package/core/collections/BTree.ts +0 -1340
- package/core/collections/Heap.ts +0 -189
- package/core/collections/LRUCache.d.ts +0 -152
- package/core/collections/LRUCache.js +0 -409
- package/core/collections/LRUCache.ts +0 -486
- package/core/collections/SortedMap.ts +0 -146
- package/core/collections/SortedSet.ts +0 -110
- package/core/collections/SortedTree.ts +0 -160
- package/core/collections/SplayTree.ts +0 -354
- package/core/collections/abc.ts +0 -176
- package/core/collections/deque.ts +0 -338
- package/core/collections/frozenset.ts +0 -37
- package/core/collections/index.ts +0 -1024
- package/core/collections/test.html +0 -10
- package/core/collections/typedArrays.d.ts +0 -31
- package/core/collections/typedArrays.js +0 -143
- package/core/collections/typedArrays.ts +0 -163
- package/core/functools/index.ts +0 -260
- package/core/globals/index.ts +0 -481
- package/core/i18n/date/format.d.ts +0 -16
- package/core/i18n/date/format.js +0 -307
- package/core/i18n/date/format.ts +0 -343
- package/core/i18n/date/index.d.ts +0 -224
- package/core/i18n/date/index.js +0 -387
- package/core/i18n/date/index.ts +0 -471
- package/core/i18n/date/test.html +0 -67
- package/core/i18n/index.ts +0 -8
- package/core/index.ts +0 -8
- package/core/itertools/index.ts +0 -494
- package/core/math/index.ts +0 -152
- package/core/operators/index.ts +0 -310
- package/core/random/index.ts +0 -66
- package/firebase.json +0 -10
- package/jasmine.json +0 -14
- package/tests/BTree.spec.ts +0 -143
- package/tests/Cache.spec.ts +0 -161
- package/tests/Heap.spec.ts +0 -66
- package/tests/SortedMap.spec.ts +0 -144
- package/tests/SortedSet.spec.ts +0 -84
- package/tests/SortedTree.spec.ts +0 -164
- package/tests/SplayTree.spec.ts +0 -121
- package/tests/collections.spec.ts +0 -288
- package/tests/deque.spec.ts +0 -107
- package/tests/globals.spec.ts +0 -270
- package/tests/itertools.spec.ts +0 -266
- package/tests/math.spec.ts +0 -25
- package/tests/operators.spec.ts +0 -40
- package/tests/random.spec.ts +0 -46
- package/tsconfig.json +0 -126
- package/tsconfig.node.json +0 -18
- package/tsconfig.xo.json +0 -9
package/core/itertools/index.ts
DELETED
|
@@ -1,494 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module includes functions that deal with or produce iterators.
|
|
3
|
-
*
|
|
4
|
-
* @module Iterators
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
call,
|
|
9
|
-
Function,
|
|
10
|
-
isEmpty,
|
|
11
|
-
isObject,
|
|
12
|
-
Iteratee,
|
|
13
|
-
notNull,
|
|
14
|
-
Predicate,
|
|
15
|
-
ValueError
|
|
16
|
-
} from '../globals/index.js'
|
|
17
|
-
import { add, bool, compare, Comparer, eq, id } from '../operators/index.js'
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
|
|
21
|
-
* @param iterable The iterable to accumulate
|
|
22
|
-
* @param func The accumulator function (default `add`).
|
|
23
|
-
* @param initial Optional initial value (defaults to the first element in iterable)
|
|
24
|
-
* @returns A sequence of accumulated values
|
|
25
|
-
*/
|
|
26
|
-
export function* accumulate<T>(iterable: Iterable<T>, func = add, initial?) {
|
|
27
|
-
const it = iter(iterable)
|
|
28
|
-
let total = initial
|
|
29
|
-
let head = it.next()
|
|
30
|
-
if (initial == null) total = head.value
|
|
31
|
-
while (!head.done) {
|
|
32
|
-
yield total
|
|
33
|
-
head = it.next()
|
|
34
|
-
if (head.done) return
|
|
35
|
-
total = func(total, head.value)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Returns `true` when all of the items in iterable are truthy. An optional predicate function can be used to define what truthiness means for this specific collection.
|
|
41
|
-
* @param iterable
|
|
42
|
-
* @param fn
|
|
43
|
-
* @returns `True` if all elements pass the test or are truthy else `false`.
|
|
44
|
-
* @example
|
|
45
|
-
```js
|
|
46
|
-
all([0])
|
|
47
|
-
// => true
|
|
48
|
-
|
|
49
|
-
all([1, 2, 3])
|
|
50
|
-
// => true
|
|
51
|
-
|
|
52
|
-
all([2, 4, 6] => (n) => n > 1)
|
|
53
|
-
// => true
|
|
54
|
-
```
|
|
55
|
-
@see {@link any}
|
|
56
|
-
*/
|
|
57
|
-
export const all = <T>(iterable: Iterable<T>, fn: Predicate<T> = bool) => {
|
|
58
|
-
for (const item of iterable) {
|
|
59
|
-
if (!fn(item)) return false
|
|
60
|
-
}
|
|
61
|
-
return true
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Returns true when any of the items in iterable are truthy. An optional predicate function can be used to define what truthiness means for this specific collection.
|
|
66
|
-
* @param iterable
|
|
67
|
-
* @param fn
|
|
68
|
-
* @returns `True` if any element passes the test or is truthy else `false`.
|
|
69
|
-
* @example
|
|
70
|
-
```js
|
|
71
|
-
any([0])
|
|
72
|
-
// => false
|
|
73
|
-
|
|
74
|
-
any([0, 1, null, undefined])
|
|
75
|
-
// => true
|
|
76
|
-
|
|
77
|
-
any([1, 4, 5] => (n) => n % 2 === 0)
|
|
78
|
-
// => true
|
|
79
|
-
```
|
|
80
|
-
@see {@link all}
|
|
81
|
-
*/
|
|
82
|
-
export const any = <T>(iterable: Iterable<T>, fn: Predicate<T> = bool) => {
|
|
83
|
-
for (const item of iterable) {
|
|
84
|
-
if (fn(item)) return true
|
|
85
|
-
}
|
|
86
|
-
return false
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function chain<T>(...iterables: Array<Iterable<T>>) {
|
|
90
|
-
return iflatten(iterables)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Checks if the `value` is in `collection`.
|
|
95
|
-
* @param collection The collection to inspect.
|
|
96
|
-
* @param value The value to search for.
|
|
97
|
-
* @returns Returns `true` if value is found, else `false`.
|
|
98
|
-
* @example
|
|
99
|
-
```js
|
|
100
|
-
contains([1, 2, 3], 1)
|
|
101
|
-
// => true
|
|
102
|
-
|
|
103
|
-
contains([1, 2, 3], 1, 2)
|
|
104
|
-
// => false
|
|
105
|
-
|
|
106
|
-
contains({ 'a': 1, 'b': 2 }, 1)
|
|
107
|
-
// => true
|
|
108
|
-
|
|
109
|
-
contains('abcd', 'bc')
|
|
110
|
-
// => true
|
|
111
|
-
```
|
|
112
|
-
*
|
|
113
|
-
* @see {@link Collections.find}
|
|
114
|
-
*/
|
|
115
|
-
export function contains(collection: Iterable<any>, value) {
|
|
116
|
-
if (!collection) return false
|
|
117
|
-
const includes = call(collection, 'includes', value)
|
|
118
|
-
if (includes != null) return includes
|
|
119
|
-
return any(collection, (x) => eq(value, x))
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const compKey = (compare: Comparer, key) => (x, y) => compare(key(x), key(y))
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Returns a generator that drops elements from the iterable as long as the predicate is `true`; afterwards, returns every remaining element. Note, the generator does not produce any output until the predicate first becomes `false`.
|
|
126
|
-
* @param iterable The iterable to inspect.
|
|
127
|
-
* @param predicate A function invoked per element to check if it should be dropped.
|
|
128
|
-
* @returns Elements
|
|
129
|
-
*/
|
|
130
|
-
export function* dropWhile<T>(iterable: Iterable<T>, predicate: Predicate) {
|
|
131
|
-
const it = iter(iterable)
|
|
132
|
-
while (true) {
|
|
133
|
-
const head = it.next()
|
|
134
|
-
if (!predicate(head.value)) {
|
|
135
|
-
if (head.done) return head.value
|
|
136
|
-
yield head.value
|
|
137
|
-
break
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
while (true) {
|
|
141
|
-
const head = it.next()
|
|
142
|
-
if (head.done) return head.value
|
|
143
|
-
yield head.value
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function iforEach<T>(iterable: Iterable<T>, fun: Iteratee<T>) {
|
|
148
|
-
for (const [index, value] of enumerate(iterable)) {
|
|
149
|
-
if (fun(value as T, index, iterable) === false) {
|
|
150
|
-
return
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export function IterableIterator<T = any>(next) {
|
|
156
|
-
const it: Iterator<T> & Iterable<T> = {
|
|
157
|
-
next,
|
|
158
|
-
[Symbol.iterator]() {
|
|
159
|
-
return this
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return it
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
|
|
167
|
-
* @template T
|
|
168
|
-
* @param iterable The iterable to inspect.
|
|
169
|
-
* @param start The starting index.
|
|
170
|
-
* @returns A generator with tuples like `[index, item]`.
|
|
171
|
-
* @example
|
|
172
|
-
```js
|
|
173
|
-
console.log([...enumerate(['hello', 'world'])])
|
|
174
|
-
// => [[0, 'hello'], [1, 'world']]
|
|
175
|
-
|
|
176
|
-
// Start with index `1`
|
|
177
|
-
console.log([...enumerate(['hello', 'world'], 1)])
|
|
178
|
-
// => [[1, 'hello'], [2, 'world']]
|
|
179
|
-
```
|
|
180
|
-
*/
|
|
181
|
-
export function* enumerate<T = any>(iterable: Iterable<T>, start = 0) {
|
|
182
|
-
let index = start
|
|
183
|
-
for (const item of iterable) {
|
|
184
|
-
yield [index++, item]
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Returns the first element in an iterable.
|
|
190
|
-
* @param iterable The iterable to inspect.
|
|
191
|
-
* @returns The first element found or undefined if iterable is empty.
|
|
192
|
-
*/
|
|
193
|
-
export function first<T>(iterable: Iterable<T>) {
|
|
194
|
-
return iter(iterable).next().value
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Creates a new generator that filters all falsy and empty values from iterable.
|
|
199
|
-
* @param arr The array to compact
|
|
200
|
-
* @returns A new array with the filtered values.
|
|
201
|
-
* @example
|
|
202
|
-
```js
|
|
203
|
-
compact([0, 1, false, 2, '', 3])
|
|
204
|
-
// => [1, 2, 3]
|
|
205
|
-
```
|
|
206
|
-
*/
|
|
207
|
-
export function* icompact<T>(iterable: Iterable<T>) {
|
|
208
|
-
for (const value of iterable) {
|
|
209
|
-
if (!isEmpty(value)) yield value
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Iterates over elements of collection, producing only those elements where predicate returns a `truthy` value.
|
|
215
|
-
*/
|
|
216
|
-
export function* ifilter<T>(iterable: Iterable<T>, predicate: Predicate) {
|
|
217
|
-
for (const value of iterable) {
|
|
218
|
-
if (predicate(value)) yield value
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export function* imap<T, TReturn = any>(iterable: Iterable<T>, mapFn: (x: T) => TReturn) {
|
|
223
|
-
for (const item of iterable) {
|
|
224
|
-
yield mapFn(item)
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Returns a generator flattening one level of nesting in a list of lists.
|
|
230
|
-
* @param iterables The iterable to flatten
|
|
231
|
-
* @example
|
|
232
|
-
```js
|
|
233
|
-
[...iflatten([[0, 1], [2, 3]])]
|
|
234
|
-
// => [0, 1, 2, 3]
|
|
235
|
-
```
|
|
236
|
-
*/
|
|
237
|
-
export function* iflatten<T = any>(iterables: Iterable<Iterable<T>>) {
|
|
238
|
-
for (const iterator of iterables) {
|
|
239
|
-
for (const item of iterator) {
|
|
240
|
-
yield item
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export function flatMap<T>(iterable: Iterable<T>, mapFn: (x: T) => any = id) {
|
|
246
|
-
return iflatten<T>(imap(iterable, mapFn))
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
export function* islice<T>(iterable: Iterable<T>, start: number, stop?: number, step = 1) {
|
|
250
|
-
if (stop != null && stop < 0) throw new ValueError(`stop parameter can't be negative`)
|
|
251
|
-
if (start < 0) throw new ValueError(`start parameter can't be negative`)
|
|
252
|
-
if (stop != null && start > stop) throw new ValueError(`start parameter can't be after stop`)
|
|
253
|
-
if (step < 0) throw new ValueError(`step parameter can't be negative`)
|
|
254
|
-
for (const [i, value] of enumerate(iterable)) {
|
|
255
|
-
if (i === start) {
|
|
256
|
-
if (stop && start + step > stop) return value
|
|
257
|
-
yield value
|
|
258
|
-
start += step
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export function* itake<T>(n: number, iterable: Iterable<T>) {
|
|
264
|
-
const it = iter(iterable)
|
|
265
|
-
while (n-- > 0) {
|
|
266
|
-
const item = it.next()
|
|
267
|
-
if (!item.done) yield item.value
|
|
268
|
-
else return
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Returns an Iterator from the elements of a collection or the object keys.
|
|
274
|
-
* @param obj The given collection to iterate over.
|
|
275
|
-
* @returns An Iterator type.
|
|
276
|
-
*/
|
|
277
|
-
export function iter<T>(obj): Iterator<T> {
|
|
278
|
-
const iterable = call(obj, Symbol.iterator)
|
|
279
|
-
if (notNull(iterable)) return iterable as Iterator<T>
|
|
280
|
-
return Object.keys(obj)[Symbol.iterator]() as Iterator<T>
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
export function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>) {
|
|
284
|
-
const left = [] as T[]
|
|
285
|
-
const right = [] as T[]
|
|
286
|
-
for (const value of iterable) {
|
|
287
|
-
if (predicate(value)) left.push(value)
|
|
288
|
-
else right.push(value)
|
|
289
|
-
}
|
|
290
|
-
return [left, right]
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Returns a generator for a sequence of numbers in the given range, starting from `start` (default 0) to `stop` (not-inclusive) in increments of `step` (default 1).
|
|
295
|
-
*
|
|
296
|
-
* `range(x)` is a shorthand for `range(0, x, 1)`.
|
|
297
|
-
* @param args When called with a single argument, it is assigned to `stop` param. If two arguments are given, it will assign them to `(start, stop)`. When three arguments, it means `(start, stop, step)`.
|
|
298
|
-
* @example
|
|
299
|
-
```js
|
|
300
|
-
range(5)
|
|
301
|
-
// => [0, 1, 2, 3, 4]
|
|
302
|
-
|
|
303
|
-
range(2, 5)
|
|
304
|
-
// => [2, 3, 4]
|
|
305
|
-
|
|
306
|
-
range(0, 5, 2)
|
|
307
|
-
// => [0, 2, 4]
|
|
308
|
-
|
|
309
|
-
range(5, 0, -1)
|
|
310
|
-
// => [5, 4, 3, 2, 1]
|
|
311
|
-
|
|
312
|
-
range(-3)
|
|
313
|
-
// => []
|
|
314
|
-
```
|
|
315
|
-
*/
|
|
316
|
-
export function* range(...args: number[]) {
|
|
317
|
-
let start = 0
|
|
318
|
-
let stop = 0
|
|
319
|
-
let step = 1
|
|
320
|
-
if (args.length === 1) {
|
|
321
|
-
stop = args[0]
|
|
322
|
-
if (stop < 0) step = -1
|
|
323
|
-
} else if (args.length === 2) {
|
|
324
|
-
start = args[0]
|
|
325
|
-
stop = args[1]
|
|
326
|
-
} else {
|
|
327
|
-
start = args[0]
|
|
328
|
-
stop = args[1]
|
|
329
|
-
step = args[2] || 1
|
|
330
|
-
}
|
|
331
|
-
for (; step > 0 ? start < stop : start > stop; start += step) {
|
|
332
|
-
yield start
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Applies a `reducer` function on each element of the iterable, in order, passing the return value from the operation on the previous element. The final result of running the reducer across all elements of the iterable is a single value.
|
|
338
|
-
* @param iterable The collection to reduce.
|
|
339
|
-
* @param reducer A reducer function that will be called per item.
|
|
340
|
-
* @param initial Optional initial value (defaults to the first element in the iterable).
|
|
341
|
-
* @returns The result of calling reducer on each item.
|
|
342
|
-
* @example
|
|
343
|
-
```js
|
|
344
|
-
reduce([1, 2, 3, 4, 5], (x, y) => x + y, 0)
|
|
345
|
-
// => 15
|
|
346
|
-
```
|
|
347
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce Array.reduce()}
|
|
348
|
-
*/
|
|
349
|
-
export function reduce<T>(
|
|
350
|
-
iterable: Iterable<T>,
|
|
351
|
-
reducer: (prev: T, value: T, index: any) => T,
|
|
352
|
-
initial?: any
|
|
353
|
-
) {
|
|
354
|
-
const it = iter<T>(iterable)
|
|
355
|
-
let output = initial ?? it.next().value
|
|
356
|
-
for (const [index, value] of enumerate(it as any)) {
|
|
357
|
-
output = reducer(output, value as T, index as number)
|
|
358
|
-
}
|
|
359
|
-
return output
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Creates a new array with the elements from the iterable in reverse order.
|
|
364
|
-
* @param iterable The iterable to inspect.
|
|
365
|
-
* @returns A new array with the elements reversed.
|
|
366
|
-
*/
|
|
367
|
-
export function reversed<T>(iterable: Iterable<T>) {
|
|
368
|
-
return Array.from(iterable).reverse()
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
export function sorted(args: any[]): any[]
|
|
372
|
-
export function sorted(args: any[], reverse: boolean): any[]
|
|
373
|
-
export function sorted(args: any[], reverse: boolean, comp: Comparer): any[]
|
|
374
|
-
export function sorted(args: any[], key: Function): any[]
|
|
375
|
-
export function sorted(args: any[], key: Function, reverse: boolean): any[]
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Creates a new sorted list from the elements in the array. This can be called many ways:
|
|
379
|
-
* - sorted([...], true) => reverse order
|
|
380
|
-
* - sorted([...], fn = (x) => any) => using a key fn
|
|
381
|
-
* - sorted([...], fn = (x) => any, true) => reverse order using key
|
|
382
|
-
* - sorted([...], false, (x, y) => number) => using custom compare
|
|
383
|
-
* - sorted([...], fn = (x) => any, true, (x, y) => number) => custom key, reverse and compare
|
|
384
|
-
*
|
|
385
|
-
* If `args` is an Object, returns the sorted keys.
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
* @param {(Array | Object)} args
|
|
389
|
-
* @param {(boolean | Function)} [key]
|
|
390
|
-
* @param {(boolean | Comparer)} [reverse]
|
|
391
|
-
* @param {Comparer} [compareFn]
|
|
392
|
-
* @return
|
|
393
|
-
*/
|
|
394
|
-
export function sorted(
|
|
395
|
-
args,
|
|
396
|
-
key?: boolean | Function,
|
|
397
|
-
reverse?: boolean | Comparer,
|
|
398
|
-
compareFn?: Comparer
|
|
399
|
-
) {
|
|
400
|
-
if (isObject(args)) args = Object.keys(args)
|
|
401
|
-
if (typeof key === 'boolean') {
|
|
402
|
-
if (typeof reverse === 'function') {
|
|
403
|
-
compareFn = reverse
|
|
404
|
-
reverse = false
|
|
405
|
-
} else {
|
|
406
|
-
reverse = true
|
|
407
|
-
}
|
|
408
|
-
key = id
|
|
409
|
-
}
|
|
410
|
-
const copy = Array.from(args)
|
|
411
|
-
const _compare = compKey(compareFn || compare, key || id)
|
|
412
|
-
copy.sort(_compare)
|
|
413
|
-
if (reverse) copy.reverse()
|
|
414
|
-
return copy
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
export function sort(args: any[]): any[]
|
|
418
|
-
export function sort(args: any[], reverse: boolean): any[]
|
|
419
|
-
export function sort(args: any[], compareFn: Comparer): any[]
|
|
420
|
-
export function sort(args: any[], reverse: boolean, compareFn: Comparer): any[]
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Sort `args` in place. Can be called like this:
|
|
424
|
-
* - sort([...], true) => reverse order
|
|
425
|
-
* - sort([...], (x, y) => number) => using custom compare
|
|
426
|
-
* - sort([...], true, (x, y) => number) => reverse and using custom compare
|
|
427
|
-
*
|
|
428
|
-
* @param {any[]} args
|
|
429
|
-
* @param {(boolean | Comparer)} [reverse]
|
|
430
|
-
* @param {Comparer} [compareFn]
|
|
431
|
-
* @return
|
|
432
|
-
*/
|
|
433
|
-
export function sort(args: any[], reverse?: boolean | Comparer, compareFn?: Comparer) {
|
|
434
|
-
if (typeof reverse === 'function') {
|
|
435
|
-
compareFn = reverse
|
|
436
|
-
reverse = false
|
|
437
|
-
}
|
|
438
|
-
args.sort(compareFn || compare)
|
|
439
|
-
if (reverse === true) args.reverse()
|
|
440
|
-
return args
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
export function take<T>(n: number, iterable: Iterable<T>) {
|
|
444
|
-
return [...itake<T>(n, iterable)]
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* Returns a generator that takes elements from the iterable as long as the predicate is `true`.
|
|
449
|
-
* @param iterable The iterable to inspect.
|
|
450
|
-
* @param predicate A function invoked per element to check if it should be taken.
|
|
451
|
-
* @returns Elements
|
|
452
|
-
*/
|
|
453
|
-
export function* takeWhile<T>(iterable: Iterable<T>, predicate: Predicate) {
|
|
454
|
-
for (const value of iterable) {
|
|
455
|
-
if (!predicate(value)) return
|
|
456
|
-
yield value
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/** Returns a generator that calls `fn(index)` for each index < n */
|
|
461
|
-
/**
|
|
462
|
-
* Returns a generator that produces `fn(index)` for each `index < n`.
|
|
463
|
-
* @param n The number of times to invoke `fn`.
|
|
464
|
-
* @param fn A function used to produce the results.
|
|
465
|
-
*/
|
|
466
|
-
export function* times(n: number, fn: (index: number) => any) {
|
|
467
|
-
for (let i = 0; i < n; i++) yield fn(i)
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* Returns a generator that aggregates elements from the iterables producing tuples with the next element from each one.
|
|
472
|
-
* @param args The iterables to zip
|
|
473
|
-
* @returns Tuples with elements from each iterator
|
|
474
|
-
* @example
|
|
475
|
-
```js
|
|
476
|
-
for item of zip([1, 2, 3], ['sugar', 'spice', 'nice'])
|
|
477
|
-
console.log(item)
|
|
478
|
-
// => [1, 'sugar']
|
|
479
|
-
// => [2, 'spice']
|
|
480
|
-
// => [3, 'nice']
|
|
481
|
-
```
|
|
482
|
-
*/
|
|
483
|
-
export function* zip(...args: Array<Iterable<any>>) {
|
|
484
|
-
const iterables = args.map(iter)
|
|
485
|
-
while (true) {
|
|
486
|
-
const items = [] as any[]
|
|
487
|
-
for (const iterator of iterables) {
|
|
488
|
-
const { value, done } = iterator.next()
|
|
489
|
-
if (done) return
|
|
490
|
-
items.push(value)
|
|
491
|
-
}
|
|
492
|
-
yield items
|
|
493
|
-
}
|
|
494
|
-
}
|
package/core/math/index.ts
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Math and algebraic operations.
|
|
3
|
-
*
|
|
4
|
-
* @module Math
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { call, Predicate } from '../globals/index.js'
|
|
8
|
-
import { reduce } from '../itertools/index.js'
|
|
9
|
-
import { gt, id, lt } from '../operators/index.js'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
|
|
13
|
-
* @param x The value.
|
|
14
|
-
* @returns The absolute value.
|
|
15
|
-
*/
|
|
16
|
-
export function abs(x): number {
|
|
17
|
-
const op = call(x, 'abs')
|
|
18
|
-
if (op != null) return op
|
|
19
|
-
return Math.abs(x)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Round `x` to the number of digits after the decimal point. If `digits` is
|
|
24
|
-
* omitted, it returns the nearest integer to x.
|
|
25
|
-
*
|
|
26
|
-
* @param {number} x
|
|
27
|
-
* @param {number} [digits=0]
|
|
28
|
-
*
|
|
29
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
|
|
30
|
-
*/
|
|
31
|
-
export function round(x: number, digits = 0) {
|
|
32
|
-
const base = 10 ** digits
|
|
33
|
-
return Math.round(x * base) / base
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Rounds down and returns the largest integer less than or equal to a given number.
|
|
38
|
-
* @param x A number.
|
|
39
|
-
* @returns Nearest integer less than or equal to number.
|
|
40
|
-
*
|
|
41
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor Math.floor()}
|
|
42
|
-
*/
|
|
43
|
-
export const floor = (x: number) => Math.floor(x)
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Rounds up and returns the largest integer greater than or equal to a given number.
|
|
47
|
-
* @param x A number.
|
|
48
|
-
* @returns Nearest integer greater than or equal to number.
|
|
49
|
-
*
|
|
50
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil Math.ceil()}
|
|
51
|
-
*/
|
|
52
|
-
export const ceil = (x: number) => Math.ceil(x)
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Returns a tuple like `[x / y, x % y]`.
|
|
56
|
-
*
|
|
57
|
-
* @param x
|
|
58
|
-
* @param y
|
|
59
|
-
* @returns
|
|
60
|
-
*/
|
|
61
|
-
export function divmod(x: number, y: number) {
|
|
62
|
-
return [Math.floor(x / y), x % y]
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function log2(x) {
|
|
66
|
-
if (x > 0) {
|
|
67
|
-
return Math.log(x) * 1.442695
|
|
68
|
-
}
|
|
69
|
-
return Number.NaN
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function logBase(x, y) {
|
|
73
|
-
if (x > 0 && y > 0) {
|
|
74
|
-
return Math.log(y) / Math.log(x)
|
|
75
|
-
}
|
|
76
|
-
return Number.NaN
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Returns the smallest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `lt` method, this will be called to compare.
|
|
81
|
-
* @param iterable The iterable to inspect.
|
|
82
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
83
|
-
* @returns The smallest element in the iterable.
|
|
84
|
-
*/
|
|
85
|
-
export function min(iterable: number[]): number
|
|
86
|
-
export function min<T>(iterable: Iterable<T>, key?: Predicate<T>): T
|
|
87
|
-
export function min<T>(iterable: Iterable<T>, key = id) {
|
|
88
|
-
return reduce(iterable, (x, y) => (lt(key(x), key(y)) ? x : y))
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Returns the largest element in an iterable. The optional `key` argument specifies a transform on the elements before comparing them. If the elements in the iterable are objects that implement a custom `gt` method, this will be called to compare.
|
|
93
|
-
* @param iterable The iterable to inspect.
|
|
94
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
95
|
-
* @returns The largest element in the iterable.
|
|
96
|
-
*/
|
|
97
|
-
export function max(iterable: number[]): number
|
|
98
|
-
export function max<T>(iterable: Iterable<T>, key?: Predicate<T>): T
|
|
99
|
-
export function max<T>(iterable: Iterable<T>, key = id) {
|
|
100
|
-
return reduce(iterable, (x, y) => (gt(key(x), key(y)) ? x : y))
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Computes the mean of the values in `iterable` and accepts an optional `key` function which is invoked for each element to generate the values to be averaged.
|
|
105
|
-
* @param {Array<number>|Iterable<T>} iterable
|
|
106
|
-
* @param {Function} [key] Optional iteratee invoked per element.
|
|
107
|
-
* @returns Returns the mean value or `false` if no elements.
|
|
108
|
-
* @template T
|
|
109
|
-
* @example
|
|
110
|
-
```js
|
|
111
|
-
let objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }]
|
|
112
|
-
|
|
113
|
-
mean(objects, (o) => o.n)
|
|
114
|
-
// => 5
|
|
115
|
-
```
|
|
116
|
-
*/
|
|
117
|
-
export function mean(iterable: number[]): false | number
|
|
118
|
-
export function mean<T>(iterable: Iterable<T>, key?: Predicate<T>): false | number
|
|
119
|
-
export function mean<T>(iterable: Iterable<T>, key = id): false | number {
|
|
120
|
-
let total = 0
|
|
121
|
-
let count = 0
|
|
122
|
-
for (const value of iterable) {
|
|
123
|
-
total += key(value)
|
|
124
|
-
count++
|
|
125
|
-
}
|
|
126
|
-
return count !== 0 && total / count
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Sums `initial` and the items of an iterable and returns the total.
|
|
131
|
-
* @param args The elements to add to `initial`.
|
|
132
|
-
* @param initial The initial value to add to (default `0`).
|
|
133
|
-
* @returns The total sum of initial and iterable elements.
|
|
134
|
-
* @example
|
|
135
|
-
```js
|
|
136
|
-
sum([1, 2, 3, 4])
|
|
137
|
-
// => 10
|
|
138
|
-
|
|
139
|
-
sum([1, 2, 3, 4], 5)
|
|
140
|
-
// => 15
|
|
141
|
-
```
|
|
142
|
-
*/
|
|
143
|
-
export function sum(iterable: number[]): number
|
|
144
|
-
export function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T
|
|
145
|
-
export function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T
|
|
146
|
-
export function sum<T>(args: Iterable<T>, key: Predicate<T> = id) {
|
|
147
|
-
let initial = 0
|
|
148
|
-
for (const x of args) {
|
|
149
|
-
initial += key(x)
|
|
150
|
-
}
|
|
151
|
-
return initial
|
|
152
|
-
}
|