Skip to content

Commit a62cf7f

Browse files
committed
sequences.extras: simpler round-robin.
1 parent e48cd2e commit a62cf7f

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

extra/sequences/extras/extras-tests.factor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ IN: sequences.extras.tests
7272
{ 8 } [ 3 iota dup [ 1 + * ] 2map-sum ] unit-test
7373
{ 4 } [ "hello" "jello" [ = ] 2count ] unit-test
7474

75-
{ "ADEBFC" } [ { "ABC" "D" "EF" } round-robin ] unit-test
75+
{ { } } [ { } round-robin ] unit-test
76+
{ "ADEBFC" } [ { "ABC" "D" "EF" } round-robin >string ] unit-test

extra/sequences/extras/extras.factor

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,12 @@ PRIVATE>
184184
: 2count ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... n )
185185
[ 1 0 ? ] compose 2map-sum ; inline
186186

187-
:: round-robin-as ( seqs exemplar -- newseq )
188-
seqs length :> len
189-
0 0 seqs sum-lengths [
190-
f [
191-
drop dup len >= [ drop 1 + 0 ] when
192-
2dup seqs nth-unsafe ?nth
193-
[ 1 + ] [ dup not ] bi*
194-
] loop
195-
] exemplar replicate-as 2nip ;
196-
197-
: round-robin ( seqs -- newseq )
198-
[ { } ] [ dup first round-robin-as ] if-empty ;
187+
: max-lengths ( seq -- n )
188+
[ length ] [ max ] map-reduce ;
189+
190+
: round-robin ( seq -- newseq )
191+
[ { } ] [
192+
dup [ max-lengths ] [ length ] bi [ iota ] bi@
193+
[ [ 2array ] with map ] curry map concat swap
194+
[ [ first2 ] dip nth-unsafe ?nth ] curry map sift
195+
] if-empty ;

0 commit comments

Comments
 (0)