From 1bd8b19ff5627851c91fb6cd099930f396fb2898 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 1 May 2008 20:01:57 -0500 Subject: [PATCH] Rename subassoc? to assoc-subset?, add subset? word for sequences --- core/assocs/assocs-docs.factor | 4 ++-- core/assocs/assocs-tests.factor | 14 +++++++------- core/assocs/assocs.factor | 4 ++-- core/sets/sets-docs.factor | 16 +++++++++++++++- core/sets/sets.factor | 6 ++++++ 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/core/assocs/assocs-docs.factor b/core/assocs/assocs-docs.factor index de62ccd878..6170eddf52 100755 --- a/core/assocs/assocs-docs.factor +++ b/core/assocs/assocs-docs.factor @@ -68,7 +68,7 @@ ARTICLE: "assocs-lookup" "Lookup and querying of 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)." -{ $subsection subassoc? } +{ $subsection assoc-subset? } { $subsection assoc-intersect } { $subsection update } { $subsection assoc-union } @@ -215,7 +215,7 @@ HELP: assoc-all? { $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 } "." } ; -HELP: subassoc? +HELP: assoc-subset? { $values { "assoc1" assoc } { "assoc2" assoc } { "?" "a new assoc" } } { $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ; diff --git a/core/assocs/assocs-tests.factor b/core/assocs/assocs-tests.factor index 19e323bdae..30f2ec23c4 100755 --- a/core/assocs/assocs-tests.factor +++ b/core/assocs/assocs-tests.factor @@ -3,13 +3,13 @@ USING: kernel math namespaces tools.test vectors sequences sequences.private hashtables io prettyprint assocs continuations ; -[ t ] [ H{ } dup subassoc? ] unit-test -[ f ] [ H{ { 1 3 } } H{ } subassoc? ] unit-test -[ t ] [ H{ } H{ { 1 3 } } subassoc? ] unit-test -[ t ] [ H{ { 1 3 } } H{ { 1 3 } } subassoc? ] unit-test -[ f ] [ H{ { 1 3 } } H{ { 1 "hey" } } subassoc? ] unit-test -[ f ] [ H{ { 1 f } } H{ } subassoc? ] unit-test -[ t ] [ H{ { 1 f } } H{ { 1 f } } subassoc? ] unit-test +[ t ] [ H{ } dup assoc-subset? ] unit-test +[ f ] [ H{ { 1 3 } } H{ } assoc-subset? ] unit-test +[ t ] [ H{ } H{ { 1 3 } } assoc-subset? ] unit-test +[ t ] [ H{ { 1 3 } } H{ { 1 3 } } assoc-subset? ] unit-test +[ f ] [ H{ { 1 3 } } H{ { 1 "hey" } } assoc-subset? ] unit-test +[ f ] [ H{ { 1 f } } H{ } assoc-subset? ] unit-test +[ t ] [ H{ { 1 f } } H{ { 1 f } } assoc-subset? ] unit-test ! Test some combinators [ diff --git a/core/assocs/assocs.factor b/core/assocs/assocs.factor index e68c311836..92db38573a 100755 --- a/core/assocs/assocs.factor +++ b/core/assocs/assocs.factor @@ -98,11 +98,11 @@ M: assoc assoc-clone-like ( assoc exemplar -- newassoc ) : assoc-stack ( key seq -- value ) dup length 1- swap (assoc-stack) ; -: subassoc? ( assoc1 assoc2 -- ? ) +: assoc-subset? ( assoc1 assoc2 -- ? ) [ swapd at* [ = ] [ 2drop f ] if ] curry assoc-all? ; : assoc= ( assoc1 assoc2 -- ? ) - 2dup subassoc? >r swap subassoc? r> and ; + [ assoc-subset? ] [ swap assoc-subset? ] 2bi and ; : assoc-hashcode ( n assoc -- code ) [ diff --git a/core/sets/sets-docs.factor b/core/sets/sets-docs.factor index 55ef3ccddd..f4e2557a71 100644 --- a/core/sets/sets-docs.factor +++ b/core/sets/sets-docs.factor @@ -2,7 +2,7 @@ USING: kernel help.markup help.syntax sequences ; IN: sets 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 "Remove duplicates:" { $subsection prune } @@ -12,8 +12,14 @@ $nl { $subsection diff } { $subsection intersect } { $subsection union } +{ $subsection subset? } +{ $subsection set= } +"A word used to implement the above:" +{ $subsection unique } { $see-also member? memq? contains? all? "assocs-sets" } ; +ABOUT: "sets" + HELP: unique { $values { "seq" "a sequence" } { "assoc" "an assoc" } } { $description "Outputs a new assoc where the keys and values are equal." } @@ -59,3 +65,11 @@ HELP: union } ; { 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." } ; diff --git a/core/sets/sets.factor b/core/sets/sets.factor index 78a92155fc..b0d26e0f30 100644 --- a/core/sets/sets.factor +++ b/core/sets/sets.factor @@ -29,3 +29,9 @@ IN: sets : union ( seq1 seq2 -- newseq ) append prune ; + +: subset? ( seq1 seq2 -- ? ) + unique [ key? ] curry all? ; + +: set= ( seq1 seq2 -- ? ) + [ unique ] bi@ = ;