1
1
import { list } from '@iterable-iterator/list' ;
2
- import { pick } from '@iterable-iterator/map' ;
3
- import { nrepeat } from '@iterable-iterator/repeat' ;
2
+ import { map , pick } from '@iterable-iterator/map' ;
3
+
4
+ import _multicombinations from './_multicombinations.js' ;
4
5
5
6
/**
6
7
* Yields all combinations, with repetitions, of each possible choice of
@@ -18,40 +19,12 @@ import {nrepeat} from '@iterable-iterator/repeat';
18
19
* @param {number } r - The size of the combinations to generate.
19
20
* @returns {IterableIterator }
20
21
*/
21
- export default function * multicombinations ( iterable , r ) {
22
+ const multicombinations = ( iterable , r ) => {
22
23
const pool = list ( iterable ) ;
23
- const length = pool . length ;
24
-
25
- if ( length === 0 && r > 0 ) {
26
- return ;
27
- }
28
-
29
- const indices = list ( nrepeat ( 0 , r ) ) ;
30
-
31
- yield list ( pick ( pool , indices ) ) ;
32
-
33
- while ( true ) {
34
- let i = r - 1 ;
35
-
36
- // eslint-disable-next-line no-constant-condition
37
- while ( true ) {
38
- if ( i < 0 ) {
39
- return ;
40
- }
41
-
42
- if ( indices [ i ] !== length - 1 ) {
43
- const pivot = ++ indices [ i ] ;
44
-
45
- for ( ++ i ; i < r ; ++ i ) {
46
- indices [ i ] = pivot ;
47
- }
48
-
49
- break ;
50
- }
51
-
52
- -- i ;
53
- }
24
+ return map (
25
+ ( indices ) => list ( pick ( pool , indices ) ) ,
26
+ _multicombinations ( pool . length , r ) ,
27
+ ) ;
28
+ } ;
54
29
55
- yield list ( pick ( pool , indices ) ) ;
56
- }
57
- }
30
+ export default multicombinations ;
0 commit comments