Skip to content

Commit b7fa76a

Browse files
🚧 progress: Import existing sources and test from js-itertools.
1 parent 2ad1ae9 commit b7fa76a

File tree

20 files changed

+10551
-8
lines changed

20 files changed

+10551
-8
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,16 @@
6464
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6565
"test": "ava"
6666
},
67-
"dependencies": {},
67+
"dependencies": {
68+
"@iterable-iterator/iter": "^0.0.2"
69+
},
6870
"devDependencies": {
6971
"@babel/core": "7.14.0",
7072
"@babel/preset-env": "7.14.0",
7173
"@babel/register": "7.13.16",
7274
"@commitlint/cli": "12.1.1",
7375
"@js-library/commitlint-config": "0.0.4",
76+
"@total-order/primitive": "^0.0.2",
7477
"ava": "3.15.0",
7578
"babel-plugin-transform-remove-console": "6.9.4",
7679
"babel-plugin-unassert": "3.0.1",

src/_reduce.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Applies the accumulator function iteratively on the last return value of the
3+
* accumulator and the next value in the input iterable. The initial value is
4+
* the initializer parameter.
5+
*
6+
* @example
7+
* _reduce( ( x , y ) => x + y , range( 10 ) , 0 ) ; // returns 45
8+
*
9+
* @example
10+
* _reduce( ( x , y ) => x + y , range( 10 ) , 100 ) ; // returns 145
11+
*
12+
* @param {Function} accumulator - The accumulator, a 2-ary function.
13+
* @param {Iterable} iterable - The input iterable.
14+
* @param {Object} initializer - The initial value of the reduction.
15+
* @returns {Object} - The reduction of the elements of <code>iterable</code>.
16+
*/
17+
export default function _reduce(accumulator, iterable, initializer) {
18+
for (const item of iterable) {
19+
initializer = accumulator(initializer, item);
20+
}
21+
22+
return initializer;
23+
}

src/_sum.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Sums the elements of the input iterable. An optional initializer parameter
3+
* allows to start the sum of the elements at a chosen value.
4+
*
5+
* @example
6+
* _sum( range( 10 ) , 0 ) ; // returns 45
7+
*
8+
* @example
9+
* _sum( range( 10 ) , 100 ) ; // returns 145
10+
*
11+
* @param {Iterable} iterable - The input iterable.
12+
* @param {Object} initializer - The initial value of the sum.
13+
* @returns {Object} - The sum of the initializer with the elements of
14+
* <code>iterable</code>.
15+
*
16+
*/
17+
export default function _sum(iterable, initializer) {
18+
for (const item of iterable) {
19+
initializer += item;
20+
}
21+
22+
return initializer;
23+
}

src/all.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Returns true if all of the elements of the input iterable are truthy.
3+
*
4+
* @example
5+
* all( repeat( true ) ) ; // loops forever
6+
*
7+
* @example
8+
* all( repeat( false ) ) ; // returns false
9+
*
10+
* @example
11+
* all( chain( [ nrepeat( true , 10 ) , repeat( false ) ) ) ; // returns false
12+
*
13+
* @param {Iterable} iterable - The input iterable.
14+
* @returns {Boolean} Returns <code>true</code> if all element of
15+
* <code>iterable</code> are truthy, <code>false</code> otherwise.
16+
*/
17+
export default function all(iterable) {
18+
for (const item of iterable) {
19+
if (!item) {
20+
return false;
21+
}
22+
}
23+
24+
return true;
25+
}

src/any.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Returns true if any of the elements of the input iterable is truthy.
3+
*
4+
* @example
5+
* any( repeat( true ) ) ; // returns true
6+
*
7+
* @example
8+
* any( repeat( false ) ) ; // loops forever
9+
*
10+
* @example
11+
* any( nrepeat( false , 10 ) ) ; // returns false
12+
*
13+
* @param {Iterable} iterable - The input iterable.
14+
* @returns {Boolean} Returns <code>true</code> if any element of
15+
* <code>iterable</code> is truthy, <code>false</code> otherwise.
16+
*/
17+
export default function any(iterable) {
18+
for (const item of iterable) {
19+
if (item) {
20+
return true;
21+
}
22+
}
23+
24+
return false;
25+
}

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as _reduce} from './_reduce.js';
2+
export {default as _sum} from './_sum.js';
3+
export {default as all} from './all.js';
4+
export {default as any} from './any.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 some} from './some.js';
9+
export {default as sum} from './sum.js';

src/max.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {iter} from '@iterable-iterator/iter';
2+
3+
/**
4+
* Returns the largest element of the input iterable according
5+
* to some comparison function.
6+
*
7+
* @example
8+
* max( ( a , b ) => a - b , range( 10 ) ) ; // returns 9
9+
*
10+
* @example
11+
* max( ( a , b ) => a - b , range( 0 ) ) ; // returns undefined
12+
*
13+
* @param {Function} compare - The comparison function to use. This function
14+
* must be 2-ary. It must return -1, 0, or 1 depending whether the first
15+
* parameter is, respectively, less than, equal to, or greater than the second
16+
* parameter.
17+
* @param {Iterable} iterable - The input iterable.
18+
* @param {Object} [dflt=undefined] - The default value to return in the case
19+
* that the input iterable is empty.
20+
* @returns {Object} The largest element of <code>iterable</code> according to
21+
* <code>compare</code>.
22+
*/
23+
export default function max(compare, iterable, dflt = undefined) {
24+
const iterator = iter(iterable);
25+
26+
const first = iterator.next();
27+
28+
if (first.done) {
29+
return dflt;
30+
}
31+
32+
let largest = first.value;
33+
34+
for (const candidate of iterator) {
35+
if (compare(candidate, largest) > 0) {
36+
largest = candidate;
37+
}
38+
}
39+
40+
return largest;
41+
}

src/min.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {iter} from '@iterable-iterator/iter';
2+
3+
/**
4+
* Returns the smallest element of the input iterable according
5+
* to some comparison function.
6+
*
7+
* @example
8+
* min( ( a , b ) => a - b , range( 10 ) ) ; // returns 0
9+
*
10+
* @example
11+
* min( ( a , b ) => a - b , range( 0 ) ) ; // returns undefined
12+
*
13+
* @param {Function} compare - The comparison function to use. This function
14+
* must be 2-ary. It must return -1, 0, or 1 depending whether the first
15+
* parameter is, respectively, less than, equal to, or greater than the second
16+
* parameter.
17+
* @param {Iterable} iterable - The input iterable.
18+
* @param {Object} [dflt=undefined] - The default value to return in the case
19+
* that the input iterable is empty.
20+
* @returns {Object} The smallest element of <code>iterable</code> according to
21+
* <code>compare</code>.
22+
*/
23+
export default function min(compare, iterable, dflt = undefined) {
24+
const iterator = iter(iterable);
25+
26+
const first = iterator.next();
27+
28+
if (first.done) {
29+
return dflt;
30+
}
31+
32+
let smallest = first.value;
33+
34+
for (const candidate of iterator) {
35+
if (compare(candidate, smallest) < 0) {
36+
smallest = candidate;
37+
}
38+
}
39+
40+
return smallest;
41+
}

src/reduce.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import _reduce from './_reduce.js';
2+
import {iter} from '@iterable-iterator/iter';
3+
4+
/**
5+
* Applies the accumulator function iteratively on the last return value of the
6+
* accumulator and the next value in the input iterable. The initial value is
7+
* the initializer parameter. If no initial value is given, the first element
8+
* of the input iterable is used.
9+
*
10+
* @example
11+
* _reduce( ( x , y ) => x + y , range( 10 ) , 0 ) ; // returns 45
12+
*
13+
* @example
14+
* _reduce( ( x , y ) => x + y , range( 10 ) , 100 ) ; // returns 145
15+
*
16+
* @param {Function} accumulator - The accumulator, a 2-ary function.
17+
* @param {Iterable} iterable - The input iterable.
18+
* @param {Object} [initializer=undefined] - The initial value of the reduction.
19+
* @returns {Object} - The reduction of the elements of <code>iterable</code>.
20+
*/
21+
export default function reduce(accumulator, iterable, initializer = undefined) {
22+
if (initializer === undefined) {
23+
const iterator = iter(iterable);
24+
const first = iterator.next();
25+
26+
if (first.done) {
27+
return undefined;
28+
}
29+
30+
return _reduce(accumulator, iterator, first.value);
31+
}
32+
33+
return _reduce(accumulator, iterable, initializer);
34+
}

src/some.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Returns true if at least some of the elements of the input iterable are
3+
* truthy.
4+
*
5+
* @example
6+
* some( repeat( true ) , 100 ) ; // returns true
7+
*
8+
* @example
9+
* some( repeat( false ) , 0 ) ; // returns true
10+
*
11+
* @example
12+
* some( repeat( false ) , 10 ) ; // loops forever
13+
*
14+
* @example
15+
* some( nrepeat( true , 10 ) , 11 ) ; // returns false
16+
*
17+
* @param {Iterable} iterable - The input iterable.
18+
* @param {Number} n - The number of elements that should be truthy.
19+
* @returns {Boolean} Returns <code>true</code> if at least <code>n</code>
20+
* elements of <code>iterable</code> are truthy, <code>false</code> otherwise.
21+
*/
22+
23+
export default function some(iterable, n) {
24+
if (n <= 0) {
25+
return true;
26+
}
27+
28+
for (const item of iterable) {
29+
if (item && --n === 0) {
30+
return true;
31+
}
32+
}
33+
34+
return false;
35+
}

0 commit comments

Comments
 (0)