From a7bc3d426eec66623a66556cd849def2794bab73 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Mon, 14 May 2012 15:47:44 -0700 Subject: [PATCH] sets: adding ?adjoin. --- core/sets/sets-docs.factor | 5 +++++ core/sets/sets.factor | 3 +++ 2 files changed, 8 insertions(+) diff --git a/core/sets/sets-docs.factor b/core/sets/sets-docs.factor index 4067948c0e..0a53ec9829 100644 --- a/core/sets/sets-docs.factor +++ b/core/sets/sets-docs.factor @@ -87,6 +87,11 @@ HELP: adjoin } { $side-effects "set" } ; +HELP: ?adjoin +{ $values { "elt" object } { "set" set } { "?" "a boolean" } } +{ $description "A version of " { $link adjoin } " which returns whether the element was added to the set." } +{ $notes "This is slightly less efficient than " { $link adjoin } " due to the initial membership test." } ; + HELP: delete { $values { "elt" object } { "set" set } } { $description "Destructively removes " { $snippet "elt" } " from " { $snippet "set" } ". If the element is not present, this does nothing." $nl "Each mutable set type is expected to implement a method on this generic word." } diff --git a/core/sets/sets.factor b/core/sets/sets.factor index 0e08bb2bfc..6fdd4d3431 100644 --- a/core/sets/sets.factor +++ b/core/sets/sets.factor @@ -132,6 +132,9 @@ M: sequence cardinality : without ( seq set -- subseq ) tester [ not ] compose filter ; +: ?adjoin ( elt set -- ? ) + 2dup in? [ 2drop f ] [ adjoin t ] if ; inline + ! Temporarily for compatibility : unique ( seq -- assoc )