Skip to content

Commit d19055d

Browse files
import old itertools code
1 parent be4a80e commit d19055d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1519
-0
lines changed

js/src/map/chain.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
var chain = function ( iterables, out ) {
3+
4+
var i, j, n, len, it;
5+
6+
len = iterables.length;
7+
8+
9+
if ( len === 0 ) {
10+
return out;
11+
}
12+
13+
14+
for ( i = 0 ; i < len ; ++i ) {
15+
16+
it = iterables[i];
17+
n = it.length;
18+
19+
for ( j = 0 ; j < n ; ++j ) {
20+
out.push( it[j] );
21+
}
22+
}
23+
24+
return out;
25+
26+
};
27+
28+
exports.chain = chain;

js/src/map/combinations.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var combinations = function ( iterable, repeat, out ) {
2+
3+
// combinations('ABCD', 2) --> AB AC AD BC BD CD
4+
// combinations(range(4), 3) --> 012 013 023 123
5+
6+
var pool, len, indices, i, j;
7+
8+
pool = iterable;
9+
len = pool.length;
10+
11+
if ( repeat > len ) {
12+
return out;
13+
}
14+
15+
indices = range( 0, repeat, 1, [] );
16+
17+
out.push( pick( pool, indices, [] ) );
18+
19+
if ( repeat === 0 || len === 0 ) {
20+
return out;
21+
}
22+
23+
for ( ; ; ) {
24+
25+
for ( i = repeat - 1 ; i >= 0 ; --i ) {
26+
if ( indices[i] !== i + len - repeat ) {
27+
break;
28+
}
29+
}
30+
31+
if ( i < 0 ) {
32+
return out;
33+
}
34+
35+
++indices[i];
36+
37+
for ( j = i + 1 ; j < repeat ; ++j ) {
38+
indices[j] = indices[j - 1] + 1;
39+
}
40+
41+
out.push( pick( pool, indices, [] ) );
42+
43+
}
44+
45+
};
46+
47+
exports.combinations = combinations;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
3+
var combinationswithrepetition = function ( iterable, r, out ) {
4+
5+
// combinationswithrepetition('ABC', 2) --> AA AB AC BB BC CC
6+
7+
var pool, len, indices, i, next;
8+
9+
pool = iterable;
10+
len = pool.length;
11+
12+
if ( len === 0 && r > 0) {
13+
return out;
14+
}
15+
16+
indices = repeat( 0, r, [] );
17+
18+
out.push( pick( pool, indices, [] ) );
19+
20+
for ( ; ; ) {
21+
22+
for ( i = r - 1 ; i >= 0 ; --i ) {
23+
if ( indices[i] !== len - 1 ) {
24+
next = indices[i] + 1;
25+
break;
26+
}
27+
}
28+
29+
if ( i < 0 ) {
30+
return out;
31+
}
32+
33+
for ( ; i < r ; ++i ) {
34+
indices[i] = next;
35+
}
36+
37+
out.push( pick( pool, indices, [] ) );
38+
}
39+
40+
};
41+
42+
exports.combinationswithrepetition = combinationswithrepetition;

js/src/map/compress.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
var compress = function ( iterable, selector, out ) {
4+
5+
var i, j;
6+
7+
j = Math.min( iterable.length, selector.length );
8+
9+
for ( i = 0 ; i < j ; ++i ) {
10+
if ( selector[i] ) {
11+
out.push( iterable[i] );
12+
}
13+
}
14+
15+
return out;
16+
17+
};
18+
19+
exports.compress = compress;

js/src/map/dropwhile.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
var dropwhile = function ( predicate, iterable, out ) {
3+
4+
var i, n, e;
5+
6+
n = iterable.length;
7+
8+
if ( n === 0 ) {
9+
return out;
10+
}
11+
12+
i = 0;
13+
e = iterable[i];
14+
15+
while ( predicate( e ) ) {
16+
++i;
17+
18+
if ( i === n ) {
19+
return out;
20+
}
21+
22+
e = iterable[i];
23+
}
24+
25+
out.push( e );
26+
27+
for ( ++i ; i < n ; ++i ) {
28+
out.push( iterable[i] );
29+
}
30+
31+
return out;
32+
33+
};
34+
35+
exports.dropwhile = dropwhile;

js/src/map/enumerate.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
var enumerate = function ( iterable, out ) {
3+
4+
zip( [range( 0, iterable.length, 1, [] ), iterable], out );
5+
6+
return out;
7+
};
8+
9+
exports.enumerate = enumerate;

js/src/map/filter.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
var filter = function ( predicate, iterable, out ) {
3+
4+
var i, len, item;
5+
6+
len = iterable.length;
7+
8+
for ( i = 0 ; i < len ; ++i ) {
9+
10+
item = iterable[i];
11+
12+
if ( predicate( item ) ) {
13+
out.push( item );
14+
}
15+
16+
}
17+
18+
return out;
19+
20+
};
21+
22+
exports.filter = filter;
23+
exports.filtertrue = filter;

js/src/map/filterfalse.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
var filterfalse = function ( predicate, iterable, out ) {
3+
4+
var i, len, item;
5+
6+
len = iterable.length;
7+
8+
for ( i = 0 ; i < len ; ++i ) {
9+
10+
item = iterable[i];
11+
12+
if ( ! predicate( item ) ) {
13+
out.push( item )
14+
}
15+
16+
}
17+
18+
return out;
19+
20+
};
21+
22+
exports.filterfalse = filterfalse;

js/src/map/group.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
var group = function ( key, iterable, out ) {
3+
4+
var i, len, curr, next, tuple, item, items;
5+
6+
len = iterable.length;
7+
8+
if ( len === 0 ) {
9+
return out;
10+
}
11+
12+
i = 0;
13+
item = iterable[i];
14+
next = key( item );
15+
16+
do {
17+
curr = next;
18+
items = [item];
19+
tuple = [curr, items];
20+
21+
++i;
22+
23+
while ( i < len ) {
24+
25+
item = iterable[i];
26+
next = key( item );
27+
28+
if ( next !== curr ) {
29+
break;
30+
}
31+
32+
items.push( item );
33+
++i;
34+
}
35+
36+
out.push( tuple );
37+
38+
} while ( i < len );
39+
40+
41+
return out;
42+
43+
};
44+
45+
exports.group = group;
46+
exports.groupby = group;

js/src/map/map.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/**
3+
*
4+
* Maps a callable object over an array.
5+
*
6+
* /!\ currently only supports a (function, array) tuple as argument
7+
*
8+
*/
9+
10+
var map = function ( callable, iterable, out ) {
11+
12+
var i, len;
13+
14+
len = iterable.length;
15+
16+
for ( i = 0 ; i < len ; ++i ) {
17+
out.push( callable( iterable[i] ) );
18+
}
19+
20+
return out;
21+
22+
};
23+
24+
exports.map = map;

0 commit comments

Comments
 (0)