-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 progress: Import existing source from js-itertools.
- Loading branch information
1 parent
5f1ab3a
commit d1f1881
Showing
7 changed files
with
10,071 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Cycles through the input iterable. | ||
* | ||
* @example | ||
* // returns [0,1,0,1,0,1,0] | ||
* list(head(cycle(range(2)),7)) ; | ||
* | ||
* @param {Iterable} iterable - The input iterable. | ||
* @returns {IterableIterator} | ||
* | ||
*/ | ||
export default function* cycle(iterable) { | ||
const buffer = []; | ||
|
||
for (const item of iterable) { | ||
yield item; | ||
buffer.push(item); | ||
} | ||
|
||
if (buffer.length === 0) { | ||
return; | ||
} | ||
|
||
while (true) { | ||
yield* buffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
const answer = 42; | ||
export default answer; | ||
export {default as cycle} from './cycle.js'; | ||
export {default as ncycle} from './ncycle.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Same as {@link cycle} but only cycles a limited number of times. | ||
* | ||
* @example | ||
* // returns [0,1,0,1,0,1,0,1] | ||
* list(ncycle(range(2),4)) ; | ||
* | ||
* @param {Iterable} iterable - The input iterable. | ||
* @param {Number} n - The number of times to cycle through the input iterable. | ||
* @returns {Iterator} | ||
* | ||
*/ | ||
export default function* ncycle(iterable, n) { | ||
const buffer = []; | ||
|
||
for (const item of iterable) { | ||
yield item; | ||
buffer.push(item); | ||
} | ||
|
||
if (buffer.length === 0) { | ||
return; | ||
} | ||
|
||
while (--n > 0) { | ||
yield* buffer; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import test from 'ava'; | ||
|
||
import {next} from '@aureooms/js-itertools'; | ||
|
||
import {cycle} from '../../src/index.js'; | ||
|
||
test('cycle (empty)', (t) => { | ||
// The empty use case is very important | ||
// in case of an empty input, cycle must | ||
// stop immediately (no infinite loop) | ||
|
||
const it = cycle([]); | ||
|
||
t.is(it.next().done, true, 'Cycle on empty list should be empty.'); | ||
}); | ||
|
||
test('cycle', (t) => { | ||
const a = [1, 7, 3]; | ||
|
||
const it = cycle(a); | ||
|
||
for (let i = 0; i < 1000; ++i) { | ||
for (const [j, element] of a.entries()) { | ||
t.deepEqual(next(it), element, i + '.' + j); | ||
} | ||
} | ||
}); |
Oops, something went wrong.