]> gitweb.factorcode.org Git - factor.git/commitdiff
assocs: add set-of and push-of
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 15 Aug 2022 03:37:33 +0000 (22:37 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 26 Feb 2023 23:11:03 +0000 (17:11 -0600)
core/assocs/assocs-tests.factor
core/assocs/assocs.factor

index 931da8f2e013b5f2d90bea79deb2fe0f9849c0b8..32305d1f375012a8ce830d10fc133d12e1c08c46 100644 (file)
@@ -345,3 +345,11 @@ unit-test
 { H{ { 1 4 } } } [ H{ { 1 2 } } 1 over [ sq ] ?change-at ] unit-test
 { H{ { 1 2 } } } [ H{ { 1 2 } } 2 over [ sq ] ?change-at ] unit-test
 { H{ { 1 3 } } } [ H{ { 1 2 } } 3 1 pick [ drop dup ] ?change-at drop ] unit-test
+
+{ H{ { 1 100 } } } [
+    H{ } clone 1 100 set-of
+] unit-test
+
+{ H{ { 1 V{ 100 200 } } } } [
+    H{ } clone 1 100 push-of 1 200 push-of
+] unit-test
index 7b08f3e647404f6383395e985e60592fce286113..699a7830ad8761d770b75d167db2ee68405526b1 100644 (file)
@@ -28,6 +28,9 @@ M: assoc assoc-like drop ; inline
 : maybe-set-at ( value key assoc -- changed? )
     3dup at* [ = [ 3drop f ] [ set-at t ] if ] [ 2drop set-at t ] if ;
 
+: set-of ( assoc key value -- assoc )
+    swap pick set-at ; inline
+
 <PRIVATE
 
 : assoc-operator ( assoc quot -- alist quot' )
@@ -229,6 +232,9 @@ M: assoc value-at* swap [ = nip ] curry assoc-find nip ;
 : push-at ( value key assoc -- )
     [ ?push ] change-at ;
 
+: push-of ( assoc key value -- assoc )
+    swap pick push-at ; inline
+
 : zip-as ( keys values exemplar -- assoc )
     dup sequence? [
         [ 2array ] swap 2map-as