Rename subassoc? to assoc-subset?, add subset? word for sequences
parent
79f91f6b7d
commit
1bd8b19ff5
|
@ -68,7 +68,7 @@ ARTICLE: "assocs-lookup" "Lookup and querying of assocs"
|
||||||
|
|
||||||
ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
|
ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
|
||||||
"It is often useful to use the keys of an associative mapping as a set, exploiting the constant or logarithmic lookup time of most implementations (" { $link "alists" } " being a notable exception)."
|
"It is often useful to use the keys of an associative mapping as a set, exploiting the constant or logarithmic lookup time of most implementations (" { $link "alists" } " being a notable exception)."
|
||||||
{ $subsection subassoc? }
|
{ $subsection assoc-subset? }
|
||||||
{ $subsection assoc-intersect }
|
{ $subsection assoc-intersect }
|
||||||
{ $subsection update }
|
{ $subsection update }
|
||||||
{ $subsection assoc-union }
|
{ $subsection assoc-union }
|
||||||
|
@ -215,7 +215,7 @@ HELP: assoc-all?
|
||||||
{ $values { "assoc" assoc } { "quot" "a quotation with stack effect " { $snippet "( key value -- ? )" } } { "?" "a boolean" } }
|
{ $values { "assoc" assoc } { "quot" "a quotation with stack effect " { $snippet "( key value -- ? )" } } { "?" "a boolean" } }
|
||||||
{ $description "Tests if all entries in the assoc satisfy a predicate by applying the quotation to each entry in turn. a predicate quotation to entry in the assoc. Iteration stops if an entry is found for which the quotation outputs " { $link f } ". If the assoc is empty, always outputs " { $link t } "." } ;
|
{ $description "Tests if all entries in the assoc satisfy a predicate by applying the quotation to each entry in turn. a predicate quotation to entry in the assoc. Iteration stops if an entry is found for which the quotation outputs " { $link f } ". If the assoc is empty, always outputs " { $link t } "." } ;
|
||||||
|
|
||||||
HELP: subassoc?
|
HELP: assoc-subset?
|
||||||
{ $values { "assoc1" assoc } { "assoc2" assoc } { "?" "a new assoc" } }
|
{ $values { "assoc1" assoc } { "assoc2" assoc } { "?" "a new assoc" } }
|
||||||
{ $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ;
|
{ $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ;
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,13 @@ USING: kernel math namespaces tools.test vectors sequences
|
||||||
sequences.private hashtables io prettyprint assocs
|
sequences.private hashtables io prettyprint assocs
|
||||||
continuations ;
|
continuations ;
|
||||||
|
|
||||||
[ t ] [ H{ } dup subassoc? ] unit-test
|
[ t ] [ H{ } dup assoc-subset? ] unit-test
|
||||||
[ f ] [ H{ { 1 3 } } H{ } subassoc? ] unit-test
|
[ f ] [ H{ { 1 3 } } H{ } assoc-subset? ] unit-test
|
||||||
[ t ] [ H{ } H{ { 1 3 } } subassoc? ] unit-test
|
[ t ] [ H{ } H{ { 1 3 } } assoc-subset? ] unit-test
|
||||||
[ t ] [ H{ { 1 3 } } H{ { 1 3 } } subassoc? ] unit-test
|
[ t ] [ H{ { 1 3 } } H{ { 1 3 } } assoc-subset? ] unit-test
|
||||||
[ f ] [ H{ { 1 3 } } H{ { 1 "hey" } } subassoc? ] unit-test
|
[ f ] [ H{ { 1 3 } } H{ { 1 "hey" } } assoc-subset? ] unit-test
|
||||||
[ f ] [ H{ { 1 f } } H{ } subassoc? ] unit-test
|
[ f ] [ H{ { 1 f } } H{ } assoc-subset? ] unit-test
|
||||||
[ t ] [ H{ { 1 f } } H{ { 1 f } } subassoc? ] unit-test
|
[ t ] [ H{ { 1 f } } H{ { 1 f } } assoc-subset? ] unit-test
|
||||||
|
|
||||||
! Test some combinators
|
! Test some combinators
|
||||||
[
|
[
|
||||||
|
|
|
@ -98,11 +98,11 @@ M: assoc assoc-clone-like ( assoc exemplar -- newassoc )
|
||||||
: assoc-stack ( key seq -- value )
|
: assoc-stack ( key seq -- value )
|
||||||
dup length 1- swap (assoc-stack) ;
|
dup length 1- swap (assoc-stack) ;
|
||||||
|
|
||||||
: subassoc? ( assoc1 assoc2 -- ? )
|
: assoc-subset? ( assoc1 assoc2 -- ? )
|
||||||
[ swapd at* [ = ] [ 2drop f ] if ] curry assoc-all? ;
|
[ swapd at* [ = ] [ 2drop f ] if ] curry assoc-all? ;
|
||||||
|
|
||||||
: assoc= ( assoc1 assoc2 -- ? )
|
: assoc= ( assoc1 assoc2 -- ? )
|
||||||
2dup subassoc? >r swap subassoc? r> and ;
|
[ assoc-subset? ] [ swap assoc-subset? ] 2bi and ;
|
||||||
|
|
||||||
: assoc-hashcode ( n assoc -- code )
|
: assoc-hashcode ( n assoc -- code )
|
||||||
[
|
[
|
||||||
|
|
|
@ -2,7 +2,7 @@ USING: kernel help.markup help.syntax sequences ;
|
||||||
IN: sets
|
IN: sets
|
||||||
|
|
||||||
ARTICLE: "sets" "Set-theoretic operations on sequences"
|
ARTICLE: "sets" "Set-theoretic operations on sequences"
|
||||||
"Set-theoretic operations on sequences are defined on the " { $vocab-link "sets" } " vocabulary. These operations use hashtables internally to achieve linear running time."
|
"Set-theoretic operations on sequences are defined on the " { $vocab-link "sets" } " vocabulary. All of these operations use hashtables internally to achieve linear running time."
|
||||||
$nl
|
$nl
|
||||||
"Remove duplicates:"
|
"Remove duplicates:"
|
||||||
{ $subsection prune }
|
{ $subsection prune }
|
||||||
|
@ -12,8 +12,14 @@ $nl
|
||||||
{ $subsection diff }
|
{ $subsection diff }
|
||||||
{ $subsection intersect }
|
{ $subsection intersect }
|
||||||
{ $subsection union }
|
{ $subsection union }
|
||||||
|
{ $subsection subset? }
|
||||||
|
{ $subsection set= }
|
||||||
|
"A word used to implement the above:"
|
||||||
|
{ $subsection unique }
|
||||||
{ $see-also member? memq? contains? all? "assocs-sets" } ;
|
{ $see-also member? memq? contains? all? "assocs-sets" } ;
|
||||||
|
|
||||||
|
ABOUT: "sets"
|
||||||
|
|
||||||
HELP: unique
|
HELP: unique
|
||||||
{ $values { "seq" "a sequence" } { "assoc" "an assoc" } }
|
{ $values { "seq" "a sequence" } { "assoc" "an assoc" } }
|
||||||
{ $description "Outputs a new assoc where the keys and values are equal." }
|
{ $description "Outputs a new assoc where the keys and values are equal." }
|
||||||
|
@ -59,3 +65,11 @@ HELP: union
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
{ diff intersect union } related-words
|
{ diff intersect union } related-words
|
||||||
|
|
||||||
|
HELP: subset?
|
||||||
|
{ $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
|
||||||
|
{ $description "Tests if every element of " { $snippet "seq1" } " is contained in " { $snippet "seq2" } "." } ;
|
||||||
|
|
||||||
|
HELP: set=
|
||||||
|
{ $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
|
||||||
|
{ $description "Tests if both sequences contain the same elements, disregrading order and duplicates." } ;
|
||||||
|
|
|
@ -29,3 +29,9 @@ IN: sets
|
||||||
|
|
||||||
: union ( seq1 seq2 -- newseq )
|
: union ( seq1 seq2 -- newseq )
|
||||||
append prune ;
|
append prune ;
|
||||||
|
|
||||||
|
: subset? ( seq1 seq2 -- ? )
|
||||||
|
unique [ key? ] curry all? ;
|
||||||
|
|
||||||
|
: set= ( seq1 seq2 -- ? )
|
||||||
|
[ unique ] bi@ = ;
|
||||||
|
|
Loading…
Reference in New Issue