11import { list } from '@iterable-iterator/list' ;
2- import { pick } from '@iterable-iterator/map' ;
3- import { range } from '@iterable-iterator/range' ;
2+ import { map , pick } from '@iterable-iterator/map' ;
3+
4+ import _combinations from './_combinations.js' ;
45
56/**
67 * Yields all combinations of each possible choice of <code>r</code> elements
@@ -16,42 +17,14 @@ import {range} from '@iterable-iterator/range';
1617 *
1718 * @param {Iterable } iterable - The input iterable.
1819 * @param {number } r - The size of the combinations to generate.
19- * @returns {IterableIterator }
20+ * @returns {IterableIterator<Array> }
2021 */
21- export default function * combinations ( iterable , r ) {
22+ const combinations = ( iterable , r ) => {
2223 const pool = list ( iterable ) ;
23- const length = pool . length ;
24-
25- if ( r > length ) {
26- return ;
27- }
28-
29- const indices = list ( range ( 0 , r , 1 ) ) ;
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 ] !== i + length - r ) {
43- let 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+ _combinations ( pool . length , r ) ,
27+ ) ;
28+ } ;
5429
55- yield list ( pick ( pool , indices ) ) ;
56- }
57- }
30+ export default combinations ;
0 commit comments