sequences.extras: adding join-with-as, test with strings.

windows-high-dpi
John Benediktsson 2018-02-15 16:56:37 -08:00
parent 05df842e85
commit fb0a6c6c5e
2 changed files with 12 additions and 2 deletions

View File

@ -266,3 +266,8 @@ tools.test vectors vocabs ;
{ { } } [ { } ", " join-with ] unit-test
{ { 1 } } [ { 1 } ", " join-with ] unit-test
{ { 1 ", " 2 } } [ { 1 2 } ", " join-with ] unit-test
{ "" } [ "" CHAR: _ join-with ] unit-test
{ "a" } [ "a" CHAR: _ join-with ] unit-test
{ "a_b" } [ "ab" CHAR: _ join-with ] unit-test
{ "a_b_c" } [ "abc" CHAR: _ join-with ] unit-test
{ "a_b_c_d" } [ "abcd" CHAR: _ join-with ] unit-test

View File

@ -626,6 +626,11 @@ PRIVATE>
[ '[ @ not ] find drop ] 2keep drop swap
[ dup length ] unless* tail-slice ; inline
:: join-with-as ( seq glue exemplar -- newseq )
seq length dup 1 - + 0 max exemplar new-sequence :> newseq
seq [ 2 * newseq set-nth-unsafe ] each-index
seq length 1 - [ 2 * 1 + glue swap newseq set-nth-unsafe ] each-integer
newseq ;
: join-with ( seq glue -- newseq )
[ dup length dup 1 - + 0 max ] dip <array>
[ '[ 2 * _ set-nth-unsafe ] each-index ] keep ;
over join-with-as ;