]> gitweb.factorcode.org Git - factor.git/commitdiff
sets: adding ?adjoin.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 14 May 2012 22:47:44 +0000 (15:47 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 14 May 2012 22:47:44 +0000 (15:47 -0700)
core/sets/sets-docs.factor
core/sets/sets.factor

index 4067948c0e7d0499e092c87b5b64f8c88f4f13ff..0a53ec98293f456e6a8a0e69e7dee09c3a5dca80 100644 (file)
@@ -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." }
index 0e08bb2bfcafd46ce125f5a2767a4f33b6a76dde..6fdd4d3431a5c89bf026e5a3759b319306a95877 100644 (file)
@@ -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 )