atomic-fns 0.2.3 → 0.3.0
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/.firebaserc +5 -0
- package/README.md +5 -2
- package/firebase.json +10 -0
- package/package.json +4 -3
- package/src/collections/index.d.ts +14 -0
- package/src/collections/index.js +23 -0
- package/src/collections/index.ts +24 -0
- package/src/functools/index.d.ts +78 -12
- package/src/functools/index.js +147 -12
- package/src/functools/index.ts +174 -14
- package/src/globals/index.d.ts +36 -41
- package/src/globals/index.js +28 -31
- package/src/globals/index.ts +41 -46
- package/src/itertools/index.d.ts +1 -30
- package/src/itertools/index.js +4 -40
- package/src/itertools/index.ts +3 -43
- package/src/math/index.d.ts +76 -0
- package/src/math/index.js +53 -0
- package/src/math/index.ts +110 -2
- package/src/operators/index.d.ts +3 -3
- package/src/operators/index.js +8 -8
- package/src/operators/index.ts +8 -8
- package/tests/globals.spec.ts +8 -16
- package/tests/itertools.spec.ts +6 -20
- package/tests/math.spec.ts +25 -0
package/src/itertools/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @module Iterators
|
|
5
5
|
*/
|
|
6
6
|
import { call, isEmpty, isObject, notNull, ValueError } from '../globals/index.js';
|
|
7
|
-
import { bool, comp, eq,
|
|
7
|
+
import { add, bool, comp, eq, id } from '../operators/index.js';
|
|
8
8
|
/**
|
|
9
9
|
* Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
|
|
10
10
|
* @param iterable The iterable to accumulate
|
|
@@ -99,7 +99,7 @@ contains('abcd', 'bc')
|
|
|
99
99
|
// => true
|
|
100
100
|
```
|
|
101
101
|
*
|
|
102
|
-
* @see {@link
|
|
102
|
+
* @see {@link Collections.find}
|
|
103
103
|
*/
|
|
104
104
|
export function contains(collection, value) {
|
|
105
105
|
if (!collection)
|
|
@@ -263,24 +263,6 @@ export function partition(iterable, predicate) {
|
|
|
263
263
|
}
|
|
264
264
|
return [left, right];
|
|
265
265
|
}
|
|
266
|
-
/**
|
|
267
|
-
* 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 `le` method, this will be called to compare.
|
|
268
|
-
* @param iterable The iterable to inspect.
|
|
269
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
270
|
-
* @returns The smallest element in the iterable.
|
|
271
|
-
*/
|
|
272
|
-
export function min(iterable, key = id) {
|
|
273
|
-
return reduce(iterable, (x, y) => (le(key(x), key(y)) ? x : y));
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* 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.
|
|
277
|
-
* @param iterable The iterable to inspect.
|
|
278
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
279
|
-
* @returns The largest element in the iterable.
|
|
280
|
-
*/
|
|
281
|
-
export function max(iterable, key = id) {
|
|
282
|
-
return reduce(iterable, (x, y) => (gt(key(x), key(y)) ? x : y));
|
|
283
|
-
}
|
|
284
266
|
/**
|
|
285
267
|
* 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).
|
|
286
268
|
*
|
|
@@ -310,6 +292,8 @@ export function* range(...args) {
|
|
|
310
292
|
let step = 1;
|
|
311
293
|
if (args.length === 1) {
|
|
312
294
|
stop = args[0];
|
|
295
|
+
if (stop < 0)
|
|
296
|
+
step = -1;
|
|
313
297
|
}
|
|
314
298
|
else if (args.length === 2) {
|
|
315
299
|
start = args[0];
|
|
@@ -411,26 +395,6 @@ export function sort(args, reverse, compareFn) {
|
|
|
411
395
|
args.reverse();
|
|
412
396
|
return args;
|
|
413
397
|
}
|
|
414
|
-
/**
|
|
415
|
-
* Sums `initial` and the items of an iterable and returns the total.
|
|
416
|
-
* @param args The elements to add to `initial`.
|
|
417
|
-
* @param initial The initial value to add to (default `0`).
|
|
418
|
-
* @returns The total sum of initial and iterable elements.
|
|
419
|
-
* @example
|
|
420
|
-
```js
|
|
421
|
-
sum([1, 2, 3, 4])
|
|
422
|
-
// => 10
|
|
423
|
-
|
|
424
|
-
sum([1, 2, 3, 4], 5)
|
|
425
|
-
// => 15
|
|
426
|
-
```
|
|
427
|
-
*/
|
|
428
|
-
export function sum(args, initial = 0) {
|
|
429
|
-
for (const x of args) {
|
|
430
|
-
initial += x;
|
|
431
|
-
}
|
|
432
|
-
return initial;
|
|
433
|
-
}
|
|
434
398
|
export function take(n, iterable) {
|
|
435
399
|
return [...itake(n, iterable)];
|
|
436
400
|
}
|
package/src/itertools/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
Predicate,
|
|
15
15
|
ValueError
|
|
16
16
|
} from '../globals/index.js'
|
|
17
|
-
import { bool, comp, eq,
|
|
17
|
+
import { add, bool, comp, eq, id } from '../operators/index.js'
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
|
|
@@ -110,7 +110,7 @@ contains('abcd', 'bc')
|
|
|
110
110
|
// => true
|
|
111
111
|
```
|
|
112
112
|
*
|
|
113
|
-
* @see {@link
|
|
113
|
+
* @see {@link Collections.find}
|
|
114
114
|
*/
|
|
115
115
|
export function contains(collection: Iterable<any>, value) {
|
|
116
116
|
if (!collection) return false
|
|
@@ -272,26 +272,6 @@ export function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>) {
|
|
|
272
272
|
return [left, right]
|
|
273
273
|
}
|
|
274
274
|
|
|
275
|
-
/**
|
|
276
|
-
* 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 `le` method, this will be called to compare.
|
|
277
|
-
* @param iterable The iterable to inspect.
|
|
278
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
279
|
-
* @returns The smallest element in the iterable.
|
|
280
|
-
*/
|
|
281
|
-
export function min<T>(iterable: Iterable<T>, key = id) {
|
|
282
|
-
return reduce(iterable, (x, y) => (le(key(x), key(y)) ? x : y))
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* 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.
|
|
287
|
-
* @param iterable The iterable to inspect.
|
|
288
|
-
* @param key Optional key function to transform the elements (default `id`).
|
|
289
|
-
* @returns The largest element in the iterable.
|
|
290
|
-
*/
|
|
291
|
-
export function max<T>(iterable: Iterable<T>, key = id) {
|
|
292
|
-
return reduce(iterable, (x, y) => (gt(key(x), key(y)) ? x : y))
|
|
293
|
-
}
|
|
294
|
-
|
|
295
275
|
/**
|
|
296
276
|
* 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).
|
|
297
277
|
*
|
|
@@ -321,6 +301,7 @@ export function* range(...args: number[]) {
|
|
|
321
301
|
let step = 1
|
|
322
302
|
if (args.length === 1) {
|
|
323
303
|
stop = args[0]
|
|
304
|
+
if (stop < 0) step = -1
|
|
324
305
|
} else if (args.length === 2) {
|
|
325
306
|
start = args[0]
|
|
326
307
|
stop = args[1]
|
|
@@ -436,27 +417,6 @@ export function sort(args: any[], reverse?: boolean | Comp, compareFn?: Comp) {
|
|
|
436
417
|
return args
|
|
437
418
|
}
|
|
438
419
|
|
|
439
|
-
/**
|
|
440
|
-
* Sums `initial` and the items of an iterable and returns the total.
|
|
441
|
-
* @param args The elements to add to `initial`.
|
|
442
|
-
* @param initial The initial value to add to (default `0`).
|
|
443
|
-
* @returns The total sum of initial and iterable elements.
|
|
444
|
-
* @example
|
|
445
|
-
```js
|
|
446
|
-
sum([1, 2, 3, 4])
|
|
447
|
-
// => 10
|
|
448
|
-
|
|
449
|
-
sum([1, 2, 3, 4], 5)
|
|
450
|
-
// => 15
|
|
451
|
-
```
|
|
452
|
-
*/
|
|
453
|
-
export function sum(args: Iterable<number>, initial = 0) {
|
|
454
|
-
for (const x of args) {
|
|
455
|
-
initial += x
|
|
456
|
-
}
|
|
457
|
-
return initial
|
|
458
|
-
}
|
|
459
|
-
|
|
460
420
|
export function take<T>(n: number, iterable: Iterable<T>) {
|
|
461
421
|
return [...itake<T>(n, iterable)]
|
|
462
422
|
}
|
package/src/math/index.d.ts
CHANGED
|
@@ -3,12 +3,39 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module Math
|
|
5
5
|
*/
|
|
6
|
+
import { Predicate } from '../globals/index.js';
|
|
6
7
|
/**
|
|
7
8
|
* Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
|
|
8
9
|
* @param x The value.
|
|
9
10
|
* @returns The absolute value.
|
|
10
11
|
*/
|
|
11
12
|
export declare function abs(x: any): number;
|
|
13
|
+
/**
|
|
14
|
+
* Round `x` to the number of digits after the decimal point. If `digits` is
|
|
15
|
+
* omitted, it returns the nearest integer to x.
|
|
16
|
+
*
|
|
17
|
+
* @param {number} x
|
|
18
|
+
* @param {number} [digits=0]
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
|
|
21
|
+
*/
|
|
22
|
+
export declare function round(x: number, digits?: number): number;
|
|
23
|
+
/**
|
|
24
|
+
* Rounds down and returns the largest integer less than or equal to a given number.
|
|
25
|
+
* @param x A number.
|
|
26
|
+
* @returns Nearest integer less than or equal to number.
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor Math.floor()}
|
|
29
|
+
*/
|
|
30
|
+
export declare const floor: (x: number) => number;
|
|
31
|
+
/**
|
|
32
|
+
* Rounds up and returns the largest integer greater than or equal to a given number.
|
|
33
|
+
* @param x A number.
|
|
34
|
+
* @returns Nearest integer greater than or equal to number.
|
|
35
|
+
*
|
|
36
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil Math.ceil()}
|
|
37
|
+
*/
|
|
38
|
+
export declare const ceil: (x: number) => number;
|
|
12
39
|
/**
|
|
13
40
|
* Returns a tuple like `[x / y, x % y]`.
|
|
14
41
|
*
|
|
@@ -19,3 +46,52 @@ export declare function abs(x: any): number;
|
|
|
19
46
|
export declare function divmod(x: number, y: number): number[];
|
|
20
47
|
export declare function log2(x: any): number;
|
|
21
48
|
export declare function logBase(x: any, y: any): number;
|
|
49
|
+
/**
|
|
50
|
+
* 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.
|
|
51
|
+
* @param iterable The iterable to inspect.
|
|
52
|
+
* @param key Optional key function to transform the elements (default `id`).
|
|
53
|
+
* @returns The smallest element in the iterable.
|
|
54
|
+
*/
|
|
55
|
+
export declare function min(iterable: number[]): number;
|
|
56
|
+
export declare function min<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
|
|
57
|
+
/**
|
|
58
|
+
* 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.
|
|
59
|
+
* @param iterable The iterable to inspect.
|
|
60
|
+
* @param key Optional key function to transform the elements (default `id`).
|
|
61
|
+
* @returns The largest element in the iterable.
|
|
62
|
+
*/
|
|
63
|
+
export declare function max(iterable: number[]): number;
|
|
64
|
+
export declare function max<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
|
|
65
|
+
/**
|
|
66
|
+
* 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.
|
|
67
|
+
* @param {Array<number>|Iterable<T>} iterable
|
|
68
|
+
* @param {Function} [key] Optional iteratee invoked per element.
|
|
69
|
+
* @returns Returns the mean value or `false` if no elements.
|
|
70
|
+
* @template T
|
|
71
|
+
* @example
|
|
72
|
+
```js
|
|
73
|
+
let objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }]
|
|
74
|
+
|
|
75
|
+
mean(objects, (o) => o.n)
|
|
76
|
+
// => 5
|
|
77
|
+
```
|
|
78
|
+
*/
|
|
79
|
+
export declare function mean(iterable: number[]): false | number;
|
|
80
|
+
export declare function mean<T>(iterable: Iterable<T>, key?: Predicate<T>): false | number;
|
|
81
|
+
/**
|
|
82
|
+
* Sums `initial` and the items of an iterable and returns the total.
|
|
83
|
+
* @param args The elements to add to `initial`.
|
|
84
|
+
* @param initial The initial value to add to (default `0`).
|
|
85
|
+
* @returns The total sum of initial and iterable elements.
|
|
86
|
+
* @example
|
|
87
|
+
```js
|
|
88
|
+
sum([1, 2, 3, 4])
|
|
89
|
+
// => 10
|
|
90
|
+
|
|
91
|
+
sum([1, 2, 3, 4], 5)
|
|
92
|
+
// => 15
|
|
93
|
+
```
|
|
94
|
+
*/
|
|
95
|
+
export declare function sum(iterable: number[]): number;
|
|
96
|
+
export declare function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
|
|
97
|
+
export declare function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
|
package/src/math/index.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* @module Math
|
|
5
5
|
*/
|
|
6
6
|
import { call } from '../globals/index.js';
|
|
7
|
+
import { reduce } from '../itertools/index.js';
|
|
8
|
+
import { gt, id, lt } from '../operators/index.js';
|
|
7
9
|
/**
|
|
8
10
|
* Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
|
|
9
11
|
* @param x The value.
|
|
@@ -15,6 +17,35 @@ export function abs(x) {
|
|
|
15
17
|
return op;
|
|
16
18
|
return Math.abs(x);
|
|
17
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Round `x` to the number of digits after the decimal point. If `digits` is
|
|
22
|
+
* omitted, it returns the nearest integer to x.
|
|
23
|
+
*
|
|
24
|
+
* @param {number} x
|
|
25
|
+
* @param {number} [digits=0]
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round Math.round()}
|
|
28
|
+
*/
|
|
29
|
+
export function round(x, digits = 0) {
|
|
30
|
+
const base = 10 ** digits;
|
|
31
|
+
return Math.round(x * base) / base;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Rounds down and returns the largest integer less than or equal to a given number.
|
|
35
|
+
* @param x A number.
|
|
36
|
+
* @returns Nearest integer less than or equal to number.
|
|
37
|
+
*
|
|
38
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor Math.floor()}
|
|
39
|
+
*/
|
|
40
|
+
export const floor = (x) => Math.floor(x);
|
|
41
|
+
/**
|
|
42
|
+
* Rounds up and returns the largest integer greater than or equal to a given number.
|
|
43
|
+
* @param x A number.
|
|
44
|
+
* @returns Nearest integer greater than or equal to number.
|
|
45
|
+
*
|
|
46
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil Math.ceil()}
|
|
47
|
+
*/
|
|
48
|
+
export const ceil = (x) => Math.ceil(x);
|
|
18
49
|
/**
|
|
19
50
|
* Returns a tuple like `[x / y, x % y]`.
|
|
20
51
|
*
|
|
@@ -37,3 +68,25 @@ export function logBase(x, y) {
|
|
|
37
68
|
}
|
|
38
69
|
return Number.NaN;
|
|
39
70
|
}
|
|
71
|
+
export function min(iterable, key = id) {
|
|
72
|
+
return reduce(iterable, (x, y) => (lt(key(x), key(y)) ? x : y));
|
|
73
|
+
}
|
|
74
|
+
export function max(iterable, key = id) {
|
|
75
|
+
return reduce(iterable, (x, y) => (gt(key(x), key(y)) ? x : y));
|
|
76
|
+
}
|
|
77
|
+
export function mean(iterable, key = id) {
|
|
78
|
+
let total = 0;
|
|
79
|
+
let count = 0;
|
|
80
|
+
for (const value of iterable) {
|
|
81
|
+
total += key(value);
|
|
82
|
+
count++;
|
|
83
|
+
}
|
|
84
|
+
return count !== 0 && total / count;
|
|
85
|
+
}
|
|
86
|
+
export function sum(args, key = id) {
|
|
87
|
+
let initial = 0;
|
|
88
|
+
for (const x of args) {
|
|
89
|
+
initial += key(x);
|
|
90
|
+
}
|
|
91
|
+
return initial;
|
|
92
|
+
}
|
package/src/math/index.ts
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* @module Math
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { call,
|
|
8
|
-
import {
|
|
7
|
+
import { call, Predicate } from '../globals/index.js'
|
|
8
|
+
import { reduce } from '../itertools/index.js'
|
|
9
|
+
import { gt, id, lt } from '../operators/index.js'
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
|
|
@@ -18,6 +19,38 @@ export function abs(x): number {
|
|
|
18
19
|
return Math.abs(x)
|
|
19
20
|
}
|
|
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
|
+
|
|
21
54
|
/**
|
|
22
55
|
* Returns a tuple like `[x / y, x % y]`.
|
|
23
56
|
*
|
|
@@ -42,3 +75,78 @@ export function logBase(x, y) {
|
|
|
42
75
|
}
|
|
43
76
|
return Number.NaN
|
|
44
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
|
+
}
|
package/src/operators/index.d.ts
CHANGED
|
@@ -10,12 +10,12 @@ export declare const isinstance: (x: any, y: any) => boolean;
|
|
|
10
10
|
export declare function comp(x: any, y: any): 0 | 1 | -1;
|
|
11
11
|
export declare function eq(x: any, y: any): any;
|
|
12
12
|
export declare function lt(x: any, y: any): any;
|
|
13
|
-
export declare function
|
|
13
|
+
export declare function lte(x: any, y: any): any;
|
|
14
14
|
export declare function gt(x: any, y: any): any;
|
|
15
|
-
export declare function
|
|
15
|
+
export declare function gte(x: any, y: any): any;
|
|
16
16
|
export declare function add(x: any, y: any): any;
|
|
17
17
|
export declare function sub(x: any, y: any): any;
|
|
18
|
-
export declare function
|
|
18
|
+
export declare function mul(x: any, y: any): any;
|
|
19
19
|
export declare function div(x: any, y: any): any;
|
|
20
20
|
export declare function mod(x: any, y: any): any;
|
|
21
21
|
/**
|
package/src/operators/index.js
CHANGED
|
@@ -14,9 +14,9 @@ export function comp(x, y) {
|
|
|
14
14
|
return op;
|
|
15
15
|
if (eq(x, y))
|
|
16
16
|
return 0;
|
|
17
|
-
if (
|
|
17
|
+
if (lte(x, y))
|
|
18
18
|
return -1;
|
|
19
|
-
if (
|
|
19
|
+
if (gte(x, y))
|
|
20
20
|
return 1;
|
|
21
21
|
return 0;
|
|
22
22
|
}
|
|
@@ -32,8 +32,8 @@ export function lt(x, y) {
|
|
|
32
32
|
return op;
|
|
33
33
|
return x < y;
|
|
34
34
|
}
|
|
35
|
-
export function
|
|
36
|
-
let op = call(x, '
|
|
35
|
+
export function lte(x, y) {
|
|
36
|
+
let op = call(x, 'lte', y);
|
|
37
37
|
if (op != null)
|
|
38
38
|
return op;
|
|
39
39
|
op = call(x, 'eq', y);
|
|
@@ -50,8 +50,8 @@ export function gt(x, y) {
|
|
|
50
50
|
return op;
|
|
51
51
|
return x > y;
|
|
52
52
|
}
|
|
53
|
-
export function
|
|
54
|
-
let op = call(x, '
|
|
53
|
+
export function gte(x, y) {
|
|
54
|
+
let op = call(x, 'gte', y);
|
|
55
55
|
if (op != null)
|
|
56
56
|
return op;
|
|
57
57
|
op = call(x, 'eq', y);
|
|
@@ -74,8 +74,8 @@ export function sub(x, y) {
|
|
|
74
74
|
return op;
|
|
75
75
|
return x - y;
|
|
76
76
|
}
|
|
77
|
-
export function
|
|
78
|
-
const op = call(x, '
|
|
77
|
+
export function mul(x, y) {
|
|
78
|
+
const op = call(x, 'mul', y);
|
|
79
79
|
if (op != null)
|
|
80
80
|
return op;
|
|
81
81
|
return x * y;
|
package/src/operators/index.ts
CHANGED
|
@@ -18,8 +18,8 @@ export function comp(x, y) {
|
|
|
18
18
|
const op = call(x, 'compare', y)
|
|
19
19
|
if (isNumber(op)) return op as 0 | 1 | -1
|
|
20
20
|
if (eq(x, y)) return 0
|
|
21
|
-
if (
|
|
22
|
-
if (
|
|
21
|
+
if (lte(x, y)) return -1
|
|
22
|
+
if (gte(x, y)) return 1
|
|
23
23
|
return 0
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -35,8 +35,8 @@ export function lt(x, y) {
|
|
|
35
35
|
return x < y
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
export function
|
|
39
|
-
let op = call(x, '
|
|
38
|
+
export function lte(x, y) {
|
|
39
|
+
let op = call(x, 'lte', y)
|
|
40
40
|
if (op != null) return op
|
|
41
41
|
op = call(x, 'eq', y)
|
|
42
42
|
if (op === true) return op
|
|
@@ -51,8 +51,8 @@ export function gt(x, y) {
|
|
|
51
51
|
return x > y
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
export function
|
|
55
|
-
let op = call(x, '
|
|
54
|
+
export function gte(x, y) {
|
|
55
|
+
let op = call(x, 'gte', y)
|
|
56
56
|
if (op != null) return op
|
|
57
57
|
op = call(x, 'eq', y)
|
|
58
58
|
if (op === true) return op
|
|
@@ -73,8 +73,8 @@ export function sub(x, y) {
|
|
|
73
73
|
return x - y
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
export function
|
|
77
|
-
const op = call(x, '
|
|
76
|
+
export function mul(x, y) {
|
|
77
|
+
const op = call(x, 'mul', y)
|
|
78
78
|
if (op != null) return op
|
|
79
79
|
return x * y
|
|
80
80
|
}
|
package/tests/globals.spec.ts
CHANGED
|
@@ -29,17 +29,15 @@ import {
|
|
|
29
29
|
next,
|
|
30
30
|
oct,
|
|
31
31
|
ord,
|
|
32
|
-
round,
|
|
33
32
|
str,
|
|
34
33
|
True,
|
|
35
34
|
type,
|
|
36
35
|
uniqueId,
|
|
37
|
-
values
|
|
36
|
+
values,
|
|
37
|
+
_
|
|
38
38
|
} from '../src/globals/index.js'
|
|
39
39
|
import { enumerate } from '../src/itertools/index.js'
|
|
40
40
|
|
|
41
|
-
function cb() {}
|
|
42
|
-
|
|
43
41
|
describe('globals', () => {
|
|
44
42
|
it('CustomError', () => {
|
|
45
43
|
let err = new CustomError('custom')
|
|
@@ -69,12 +67,14 @@ describe('globals', () => {
|
|
|
69
67
|
assert(type([]) === 'array')
|
|
70
68
|
assert(type({}) === 'object')
|
|
71
69
|
assert(type(type) === 'function')
|
|
70
|
+
assert(type(Error()) === 'error')
|
|
71
|
+
assert(type(new CustomError()) === 'error')
|
|
72
72
|
assert(type(Symbol()) === 'symbol')
|
|
73
73
|
assert(type(new Map()) === 'Map')
|
|
74
74
|
assert(type(new WeakMap()) === 'WeakMap')
|
|
75
75
|
assert(type(new Set()) === 'Set')
|
|
76
76
|
assert(type(new WeakSet()) === 'WeakSet')
|
|
77
|
-
assert(type(new Promise(
|
|
77
|
+
assert(type(new Promise(_)) === 'Promise')
|
|
78
78
|
assert(type(new Int8Array()) === 'Int8Array')
|
|
79
79
|
assert(type(new Uint8Array()) === 'Uint8Array')
|
|
80
80
|
assert(type(new Uint8ClampedArray()) === 'Uint8ClampedArray')
|
|
@@ -115,14 +115,14 @@ describe('globals', () => {
|
|
|
115
115
|
})
|
|
116
116
|
|
|
117
117
|
it('isFunc', () => {
|
|
118
|
-
assert(isFunc(
|
|
118
|
+
assert(isFunc(_))
|
|
119
119
|
assert(!isFunc([]))
|
|
120
120
|
})
|
|
121
121
|
|
|
122
122
|
it('isAsync', () => {
|
|
123
123
|
assert(isAsync(async function () {}))
|
|
124
124
|
assert(isAsync(async () => {}))
|
|
125
|
-
assert(!isAsync(
|
|
125
|
+
assert(!isAsync(_))
|
|
126
126
|
})
|
|
127
127
|
|
|
128
128
|
it('isNaN', () => {
|
|
@@ -143,7 +143,7 @@ describe('globals', () => {
|
|
|
143
143
|
})
|
|
144
144
|
|
|
145
145
|
it('isPromise', () => {
|
|
146
|
-
assert(isPromise(new Promise(
|
|
146
|
+
assert(isPromise(new Promise(_)))
|
|
147
147
|
})
|
|
148
148
|
|
|
149
149
|
it('isSymbol', () => {
|
|
@@ -207,14 +207,6 @@ describe('globals', () => {
|
|
|
207
207
|
assert.deepEqual(values(obj), Object.values(obj))
|
|
208
208
|
})
|
|
209
209
|
|
|
210
|
-
it('round', () => {
|
|
211
|
-
assert(round(0.5) === 1)
|
|
212
|
-
assert(round(-0.5) === 0)
|
|
213
|
-
assert(round(1.5) === 2)
|
|
214
|
-
assert(round(1.54, 2) === 1.54)
|
|
215
|
-
assert(round(1.545, 3) === 1.545)
|
|
216
|
-
})
|
|
217
|
-
|
|
218
210
|
it('next', () => {
|
|
219
211
|
const iter = enumerate([1, 2, 3])
|
|
220
212
|
assert.deepEqual(next(iter), [0, 1])
|