Skip to content

Commit 19e5dc8

Browse files
✨ feat: Add min, max, argmin, argmax, any, all, sum, and reduce.
1 parent 9eea119 commit 19e5dc8

File tree

19 files changed

+9904
-24
lines changed

19 files changed

+9904
-24
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
ArrayLike reducers for JavaScript.
55
See [docs](https://array-like.github.io/reduce/index.html).
66

7-
> :building_construction: Caveat emptor! This is work in progress. Code may be
8-
> working. Documentation may be present. Coherence may be. Maybe.
7+
```js
8+
import {shuffle} from '@randomized/random';
9+
import {_calloc} from '@array-like/alloc'
10+
import {iota} from '@array-like/fill';
11+
import {max} from '@array-like/reduce';
12+
import {increasing} from '@total-order/primitive';
913

10-
> :warning: Depending on your environment, the code may require
11-
> `regeneratorRuntime` to be defined, for instance by importing
12-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
14+
const calloc = _calloc(Int32Array);
15+
const n = 100;
16+
const a = calloc(n);
17+
iota(a, 0, n, 0);
18+
shuffle(a, 0, n);
19+
max(increasing, a, 0, n); // 99
20+
```
1321

1422
[![License](https://img.shields.io/github/license/array-like/reduce.svg)](https://raw.githubusercontent.com/array-like/reduce/main/LICENSE)
1523
[![Version](https://img.shields.io/npm/v/@array-like/reduce.svg)](https://www.npmjs.org/package/@array-like/reduce)

doc/manual/usage.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
# Usage
22

3-
> :warning: Depending on your environment, the code may require
4-
> `regeneratorRuntime` to be defined, for instance by importing
5-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
6-
7-
First, require the polyfill at the entry point of your application
8-
```js
9-
await import('regenerator-runtime/runtime.js');
10-
// or
11-
import 'regenerator-runtime/runtime.js';
12-
```
13-
14-
Then, import the library where needed
3+
Import the library where needed
154
```js
165
const reduce = await import('@array-like/reduce');
176
// or

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@
6868
},
6969
"dependencies": {},
7070
"devDependencies": {
71+
"@array-like/alloc": "^0.0.1",
7172
"@babel/core": "7.14.8",
7273
"@babel/preset-env": "7.14.8",
7374
"@babel/register": "7.14.5",
7475
"@commitlint/cli": "13.1.0",
76+
"@functional-abstraction/operator": "^3.0.0",
7577
"@js-library/commitlint-config": "0.0.4",
78+
"@randomized/random": "^4.0.0",
79+
"@total-order/primitive": "^1.0.1",
7680
"ava": "3.15.0",
7781
"babel-plugin-transform-remove-console": "6.9.4",
7882
"babel-plugin-unassert": "3.1.0",

src/all.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* All.
3+
*
4+
* @param {ArrayLike} a
5+
* @param {number} i
6+
* @param {number} j
7+
* @return {boolean}
8+
*/
9+
const all = (a, i, j) => {
10+
for (; i < j; ++i) {
11+
if (!a[i]) {
12+
return false;
13+
}
14+
}
15+
16+
return true;
17+
};
18+
19+
export default all;

src/any.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Any.
3+
*
4+
* @param {ArrayLike} a
5+
* @param {number} i
6+
* @param {number} j
7+
* @return {boolean}
8+
*/
9+
const any = (a, i, j) => {
10+
for (; i < j; ++i) {
11+
if (a[i]) {
12+
return true;
13+
}
14+
}
15+
16+
return false;
17+
};
18+
19+
export default any;

src/argmax.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Argmax.
3+
*
4+
* @param {Function} compare
5+
* @param {ArrayLike} a
6+
* @param {number} i
7+
* @param {number} j
8+
* @return {number}
9+
*/
10+
const argmax = (compare, a, i, j) => {
11+
if (i >= j) {
12+
return undefined;
13+
}
14+
15+
let argmaxSoFar = i;
16+
let maxSoFar = a[argmaxSoFar];
17+
18+
for (++i; i < j; ++i) {
19+
const candidate = a[i];
20+
21+
if (compare(candidate, maxSoFar) > 0) {
22+
argmaxSoFar = i;
23+
maxSoFar = candidate;
24+
}
25+
}
26+
27+
return argmaxSoFar;
28+
};
29+
30+
export default argmax;

src/argmin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Argmin.
3+
*
4+
* @param {Function} compare
5+
* @param {ArrayLike} a
6+
* @param {number} i
7+
* @param {number} j
8+
* @return {number}
9+
*/
10+
const argmin = (compare, a, i, j) => {
11+
if (i >= j) {
12+
return undefined;
13+
}
14+
15+
let argminSoFar = i;
16+
let minSoFar = a[argminSoFar];
17+
18+
for (++i; i < j; ++i) {
19+
const candidate = a[i];
20+
21+
if (compare(candidate, minSoFar) < 0) {
22+
argminSoFar = i;
23+
minSoFar = candidate;
24+
}
25+
}
26+
27+
return argminSoFar;
28+
};
29+
30+
export default argmin;

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as all} from './all.js';
2+
export {default as any} from './any.js';
3+
export {default as argmax} from './argmax.js';
4+
export {default as argmin} from './argmin.js';
5+
export {default as max} from './max.js';
6+
export {default as min} from './min.js';
7+
export {default as reduce} from './reduce.js';
8+
export {default as sum} from './sum.js';

src/max.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Max.
3+
*
4+
* @param {Function} compare
5+
* @param {ArrayLike} a
6+
* @param {number} i
7+
* @param {number} j
8+
* @return {any}
9+
*/
10+
const max = (compare, a, i, j) => {
11+
if (i >= j) {
12+
return undefined;
13+
}
14+
15+
let maxSoFar = a[i];
16+
17+
for (++i; i < j; ++i) {
18+
const candidate = a[i];
19+
20+
if (compare(candidate, maxSoFar) > 0) {
21+
maxSoFar = candidate;
22+
}
23+
}
24+
25+
return maxSoFar;
26+
};
27+
28+
export default max;

src/min.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Min.
3+
*
4+
* @param {Function} compare
5+
* @param {ArrayLike} a
6+
* @param {number} i
7+
* @param {number} j
8+
* @return {any}
9+
*/
10+
const min = (compare, a, i, j) => {
11+
if (i >= j) {
12+
return undefined;
13+
}
14+
15+
let minSoFar = a[i];
16+
17+
for (++i; i < j; ++i) {
18+
const candidate = a[i];
19+
20+
if (compare(candidate, minSoFar) < 0) {
21+
minSoFar = candidate;
22+
}
23+
}
24+
25+
return minSoFar;
26+
};
27+
28+
export default min;

0 commit comments

Comments
 (0)