Skip to content

Commit fe728fc

Browse files
🚧 progress: Import existing sources, tests, and code samples.
1 parent d4a1e6f commit fe728fc

File tree

7 files changed

+10208
-11
lines changed

7 files changed

+10208
-11
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
Iterable round-robin for JavaScript.
55
See [docs](https://iterable-iterator.github.io/round-robin/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.
9-
107
> :warning: Depending on your environment, the code may require
118
> `regeneratorRuntime` to be defined, for instance by importing
129
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
1310
11+
12+
```js
13+
import {roundRobin} from '@iterable-iterator/round-robin';
14+
roundRobin( [ "ABC" , "D" , "EF" ] ) ; // A D E B F C
15+
```
16+
1417
[![License](https://img.shields.io/github/license/iterable-iterator/round-robin.svg)](https://raw.githubusercontent.com/iterable-iterator/round-robin/main/LICENSE)
1518
[![Version](https://img.shields.io/npm/v/@iterable-iterator/round-robin.svg)](https://www.npmjs.org/package/@iterable-iterator/round-robin)
1619
[![Tests](https://img.shields.io/github/workflow/status/iterable-iterator/round-robin/ci:test?event=push&label=tests)](https://github.com/iterable-iterator/round-robin/actions/workflows/ci:test.yml?query=branch:main)

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,19 @@
5959
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6060
"test": "ava"
6161
},
62-
"dependencies": {},
62+
"dependencies": {
63+
"@functional-abstraction/operator": "^1.0.4",
64+
"@iterable-iterator/cycle": "^0.0.1",
65+
"@iterable-iterator/iter": "^0.0.2",
66+
"@iterable-iterator/map": "^0.0.1",
67+
"@iterable-iterator/slice": "^0.0.1"
68+
},
6369
"devDependencies": {
6470
"@babel/core": "7.14.0",
6571
"@babel/preset-env": "7.14.0",
6672
"@babel/register": "7.13.16",
6773
"@commitlint/cli": "12.1.1",
74+
"@iterable-iterator/list": "^0.0.2",
6875
"@js-library/commitlint-config": "0.0.4",
6976
"ava": "3.15.0",
7077
"babel-plugin-transform-remove-console": "6.9.4",

src/index.js

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

src/roundRobin.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {len} from '@functional-abstraction/operator';
2+
3+
import {slice} from '@iterable-iterator/slice';
4+
import {cycle} from '@iterable-iterator/cycle';
5+
import {map} from '@iterable-iterator/map';
6+
import {iter} from '@iterable-iterator/iter';
7+
8+
/**
9+
* Yields the first item of the first input iterable, then the first item of
10+
* the second input iterable, etc., until the last input iterable. Then start
11+
* again with the second item of the first input iterable, etc. If one of the
12+
* input iterable is exhausted, it is removed from the list of input iterables
13+
* and the algorithm continues until all input iterables have been exhausted.
14+
*
15+
* @example
16+
* // returns ['A','D','E','B','F','C]
17+
* list( roundrobin(['ABC', 'D', 'EF']) )
18+
*
19+
* @param {Iterable[]} iterables - The input iterables.
20+
* @returns {IterableIterator}
21+
*
22+
*/
23+
export default function* roundRobin(iterables) {
24+
let pending = len(iterables);
25+
26+
let iterators = cycle(map(iter, iterables));
27+
28+
while (pending) {
29+
while (true) {
30+
const iterator = iterators.next().value;
31+
32+
const it = iterator.next();
33+
34+
if (it.done) {
35+
break;
36+
}
37+
38+
yield it.value;
39+
}
40+
41+
--pending;
42+
43+
iterators = cycle(slice(iterators, 0, pending, 1));
44+
}
45+
}

test/src/api.js

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

test/src/roundRobin.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import test from 'ava';
2+
3+
import {list} from '@iterable-iterator/list';
4+
import {roundRobin} from '../../src/index.js';
5+
6+
const repr = (x) => JSON.stringify(x);
7+
8+
const macro = (t, iterables, expected) => {
9+
t.deepEqual(list(roundRobin(iterables)), expected);
10+
};
11+
12+
macro.title = (title, iterables, expected) =>
13+
title ?? `roundRobin(${repr(iterables)}) is ${repr(expected)}`;
14+
15+
test(macro, ['ABC', 'D', 'EF'], ['A', 'D', 'E', 'B', 'F', 'C']);

0 commit comments

Comments
 (0)