Skip to content

Commit 8319a42

Browse files
🚧 progress: Import existing source from js-itertools.
1 parent b42b963 commit 8319a42

9 files changed

Lines changed: 10080 additions & 7 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ See [docs](https://iterable-iterator.github.io/map/index.html).
1111
> `regeneratorRuntime` to be defined, for instance by importing
1212
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
1313
14+
```js
15+
import {zip, count} from '@aureooms/js-itertools';
16+
import {truth, mul} from '@aureooms/js-operator';
17+
import {map, starmap} from '@iterable-itertools/map';
18+
map( truth , [ 0 , 1 , null , undefined , "A" ] ) ; // F T F F T
19+
starmap( mul , zip( count( 0 , 1 ) , count( 0 , 1 ) ) ) ; // 0 1 4 9 16 25 36 ...
20+
```
21+
1422
[![License](https://img.shields.io/github/license/iterable-iterator/map.svg)](https://raw.githubusercontent.com/iterable-iterator/map/main/LICENSE)
1523
[![Version](https://img.shields.io/npm/v/@iterable-iterator/map.svg)](https://www.npmjs.org/package/@iterable-iterator/map)
1624
[![Tests](https://img.shields.io/github/workflow/status/iterable-iterator/map/ci:test?event=push&label=tests)](https://github.com/iterable-iterator/map/actions/workflows/ci:test.yml?query=branch:main)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
},
6262
"dependencies": {},
6363
"devDependencies": {
64+
"@aureooms/js-operator": "^1.0.2",
6465
"@babel/core": "7.13.15",
6566
"@babel/preset-env": "7.13.15",
6667
"@babel/register": "7.13.14",

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as map} from './map.js';
2+
export {default as starmap} from './starmap.js';

src/map.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Applies a given callable to each of the elements of the input iterable.
3+
*
4+
* @example
5+
* // return [ 0 , 1 , 4 , 9 ]
6+
* list( map( x => x**2 , range( 4 ) ) ) ;
7+
*
8+
* @param {Function} callable - The callable to use.
9+
* @param {Iterable} iterable - The input iterable.
10+
* @returns {IterableIterator}
11+
*/
12+
export default function* map(callable, iterable) {
13+
for (const item of iterable) {
14+
yield callable(item);
15+
}
16+
}

src/starmap.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Same as {@link map} but allows multiple arguments callable functions.
3+
*
4+
* @example
5+
* // return [ 0 , 1 , 4 , 9 ]
6+
* list( starmap( ( x , y ) => x*y , zip( range( 4 ) , range( 4 ) ) ) ;
7+
*
8+
* @param {Function} callable - The callable to use.
9+
* @param {Iterable} iterable - The input iterable.
10+
* @returns {Iterator}
11+
*/
12+
export default function* starmap(callable, iterable) {
13+
for (const args of iterable) {
14+
yield callable(...args);
15+
}
16+
}

test/src/api.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/src/map.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import test from 'ava';
2+
3+
import {map} from '../../src/index.js';
4+
5+
test('map', (t) => {
6+
const x = function (callable, iterable, out) {
7+
t.deepEqual(Array.from(map(callable, iterable)), out);
8+
};
9+
10+
const pow2 = (x) => x * x;
11+
12+
x(pow2, [], []);
13+
x(pow2, [1], [1]);
14+
x(pow2, [1, 2, 3], [1, 4, 9]);
15+
x(pow2, [1, 2, 3, 4, 5, 6], [1, 4, 9, 16, 25, 36]);
16+
x(pow2, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 4, 9, 16, 25, 36, 49, 64, 81]);
17+
});

test/src/starmap.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import test from 'ava';
2+
3+
import {mul} from '@aureooms/js-operator';
4+
import {starmap} from '../../src/index.js';
5+
6+
test('starmap', (t) => {
7+
const x = (callable, iterable, out) => {
8+
t.deepEqual(Array.from(starmap(callable, iterable)), out);
9+
};
10+
11+
x(mul, [], []);
12+
x(mul, [[1, 1]], [1]);
13+
x(
14+
mul,
15+
[
16+
[1, 1],
17+
[2, 2],
18+
[3, 3],
19+
],
20+
[1, 4, 9],
21+
);
22+
x(
23+
mul,
24+
[
25+
[1, 1],
26+
[2, 2],
27+
[3, 3],
28+
[4, 4],
29+
[5, 5],
30+
[6, 6],
31+
],
32+
[1, 4, 9, 16, 25, 36],
33+
);
34+
x(
35+
mul,
36+
[
37+
[1, 1],
38+
[2, 2],
39+
[3, 3],
40+
[4, 4],
41+
[5, 5],
42+
[6, 6],
43+
[7, 7],
44+
[8, 8],
45+
[9, 9],
46+
],
47+
[1, 4, 9, 16, 25, 36, 49, 64, 81],
48+
);
49+
});

0 commit comments

Comments
 (0)