sequences: adding reject/reject-as/reject!.
parent
a33fc84de7
commit
6071ea98f7
|
@ -513,6 +513,19 @@ HELP: filter!
|
||||||
{ $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
|
{ $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a false value." }
|
||||||
{ $side-effects "seq" } ;
|
{ $side-effects "seq" } ;
|
||||||
|
|
||||||
|
HELP: reject
|
||||||
|
{ $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "subseq" "a new sequence" } }
|
||||||
|
{ $description "Applies the quotation to each element in turn, and outputs a new sequence removing with the elements of the original sequence for which the quotation output a true value." } ;
|
||||||
|
|
||||||
|
HELP: reject-as
|
||||||
|
{ $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "exemplar" sequence } { "subseq" "a new sequence" } }
|
||||||
|
{ $description "Applies the quotation to each element in turn, and outputs a new sequence of the same type as " { $snippet "exemplar" } " remove the elements of the original sequence for which the quotation output a true value." } ;
|
||||||
|
|
||||||
|
HELP: reject!
|
||||||
|
{ $values { "seq" "a resizable mutable sequence" } { "quot" { $quotation ( ... elt -- ... ? ) } } }
|
||||||
|
{ $description "Applies the quotation to each element in turn, and removes elements for which the quotation outputs a true value." }
|
||||||
|
{ $side-effects "seq" } ;
|
||||||
|
|
||||||
HELP: interleave
|
HELP: interleave
|
||||||
{ $values { "seq" sequence } { "between" quotation } { "quot" { $quotation ( ... elt -- ... ) } } }
|
{ $values { "seq" sequence } { "between" quotation } { "quot" { $quotation ( ... elt -- ... ) } } }
|
||||||
{ $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
|
{ $description "Applies " { $snippet "quot" } " to each element in turn, also invoking " { $snippet "between" } " in-between each pair of elements." }
|
||||||
|
|
|
@ -117,6 +117,8 @@ IN: sequences.tests
|
||||||
{ t } [ B{ 0 } { 1 } append byte-array? ] unit-test
|
{ t } [ B{ 0 } { 1 } append byte-array? ] unit-test
|
||||||
{ t } [ B{ 0 } { 1 } prepend byte-array? ] unit-test
|
{ t } [ B{ 0 } { 1 } prepend byte-array? ] unit-test
|
||||||
|
|
||||||
|
{ "0123456789" } [ 58 iota [ 48 < ] "" reject-as ] unit-test
|
||||||
|
|
||||||
[ [ ] ] [ 1 [ ] remove ] unit-test
|
[ [ ] ] [ 1 [ ] remove ] unit-test
|
||||||
[ [ ] ] [ 1 [ 1 ] remove ] unit-test
|
[ [ ] ] [ 1 [ 1 ] remove ] unit-test
|
||||||
[ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
|
[ [ 3 1 1 ] ] [ 2 [ 3 2 1 2 1 ] remove ] unit-test
|
||||||
|
@ -152,6 +154,8 @@ IN: sequences.tests
|
||||||
[ 4 [ CHAR: a <string> ] { } map-integers ]
|
[ 4 [ CHAR: a <string> ] { } map-integers ]
|
||||||
unit-test
|
unit-test
|
||||||
|
|
||||||
|
{ V{ 1 3 5 7 9 } } [ 10 iota >vector [ even? ] reject! ] unit-test
|
||||||
|
|
||||||
[ V{ } ] [ "f" V{ } clone remove! ] unit-test
|
[ V{ } ] [ "f" V{ } clone remove! ] unit-test
|
||||||
[ V{ } ] [ "f" V{ "f" } clone remove! ] unit-test
|
[ V{ } ] [ "f" V{ "f" } clone remove! ] unit-test
|
||||||
[ V{ } ] [ "f" V{ "f" "f" } clone remove! ] unit-test
|
[ V{ } ] [ "f" V{ "f" "f" } clone remove! ] unit-test
|
||||||
|
|
|
@ -544,6 +544,12 @@ PRIVATE>
|
||||||
: filter ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
|
: filter ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
|
||||||
over filter-as ; inline
|
over filter-as ; inline
|
||||||
|
|
||||||
|
: reject-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... subseq )
|
||||||
|
[ [ not ] compose ] [ filter-as ] bi* ; inline
|
||||||
|
|
||||||
|
: reject ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
|
||||||
|
over reject-as ; inline
|
||||||
|
|
||||||
: push-either ( ..a elt quot: ( ..a elt -- ..b ? ) accum1 accum2 -- ..b )
|
: push-either ( ..a elt quot: ( ..a elt -- ..b ? ) accum1 accum2 -- ..b )
|
||||||
[ keep swap ] 2dip ? push ; inline
|
[ keep swap ] 2dip ? push ; inline
|
||||||
|
|
||||||
|
@ -630,16 +636,16 @@ PRIVATE>
|
||||||
[ eq? ] with any? ;
|
[ eq? ] with any? ;
|
||||||
|
|
||||||
: remove ( elt seq -- newseq )
|
: remove ( elt seq -- newseq )
|
||||||
[ = not ] with filter ;
|
[ = ] with reject ;
|
||||||
|
|
||||||
: remove-eq ( elt seq -- newseq )
|
: remove-eq ( elt seq -- newseq )
|
||||||
[ eq? not ] with filter ;
|
[ eq? ] with reject ;
|
||||||
|
|
||||||
: sift ( seq -- newseq )
|
: sift ( seq -- newseq )
|
||||||
[ ] filter ;
|
[ ] filter ;
|
||||||
|
|
||||||
: harvest ( seq -- newseq )
|
: harvest ( seq -- newseq )
|
||||||
[ empty? not ] filter ;
|
[ empty? ] reject ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
|
@ -704,11 +710,14 @@ PRIVATE>
|
||||||
: filter! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
|
: filter! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
|
||||||
swap [ [ 0 0 ] dip (filter!) ] keep ; inline
|
swap [ [ 0 0 ] dip (filter!) ] keep ; inline
|
||||||
|
|
||||||
|
: reject! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
|
||||||
|
[ not ] compose filter! ; inline
|
||||||
|
|
||||||
: remove! ( elt seq -- seq )
|
: remove! ( elt seq -- seq )
|
||||||
[ = not ] with filter! ;
|
[ = ] with reject! ;
|
||||||
|
|
||||||
: remove-eq! ( elt seq -- seq )
|
: remove-eq! ( elt seq -- seq )
|
||||||
[ eq? not ] with filter! ;
|
[ eq? ] with reject! ;
|
||||||
|
|
||||||
: prefix ( seq elt -- newseq )
|
: prefix ( seq elt -- newseq )
|
||||||
over [ over length 1 + ] dip [
|
over [ over length 1 + ] dip [
|
||||||
|
|
|
@ -170,9 +170,6 @@ IN: sequences.extras.tests
|
||||||
{ 0 "chicken" } [ { "chicken" "beef" "moose" } [ length ] supremum-by* ] unit-test
|
{ 0 "chicken" } [ { "chicken" "beef" "moose" } [ length ] supremum-by* ] unit-test
|
||||||
{ 2 "moose" } [ { "chicken" "beef" "moose" } [ first ] supremum-by* ] unit-test
|
{ 2 "moose" } [ { "chicken" "beef" "moose" } [ first ] supremum-by* ] unit-test
|
||||||
|
|
||||||
{ "0123456789" } [ 58 iota [ 48 < ] "" reject-as ] unit-test
|
|
||||||
{ V{ 1 3 5 7 9 } } [ 10 iota >vector [ even? ] reject! ] unit-test
|
|
||||||
|
|
||||||
{ 3/10 } [ 10 iota [ 3 < ] count* ] unit-test
|
{ 3/10 } [ 10 iota [ 3 < ] count* ] unit-test
|
||||||
|
|
||||||
{ { 0 } } [ "ABA" "ABABA" start-all ] unit-test
|
{ { 0 } } [ "ABA" "ABABA" start-all ] unit-test
|
||||||
|
|
|
@ -507,15 +507,6 @@ PRIVATE>
|
||||||
: infimum-by* ( ... seq quot: ( ... elt -- ... x ) -- ... i elt )
|
: infimum-by* ( ... seq quot: ( ... elt -- ... x ) -- ... i elt )
|
||||||
[ before? ] select-by* ; inline
|
[ before? ] select-by* ; inline
|
||||||
|
|
||||||
: reject-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... subseq )
|
|
||||||
[ [ not ] compose ] [ filter-as ] bi* ; inline
|
|
||||||
|
|
||||||
: reject ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
|
|
||||||
over reject-as ; inline
|
|
||||||
|
|
||||||
: reject! ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
|
|
||||||
[ not ] compose filter! ; inline
|
|
||||||
|
|
||||||
: change-last ( seq quot -- )
|
: change-last ( seq quot -- )
|
||||||
[ drop length 1 - ] [ change-nth ] 2bi ; inline
|
[ drop length 1 - ] [ change-nth ] 2bi ; inline
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue