{ H{ { 1 V{ 100 200 } } } } [
H{ } clone 1 100 push-of 1 200 push-of
] unit-test
+
+{ H{ { 123 556 } } } [
+ H{ { 123 456 } } 123 [ 100 + ] change-of
+] unit-test
+
+{ H{ { 123 556 } } } [
+ H{ { 123 456 } } 123 [ 100 + ] ?change-of
+] unit-test
+
+{ H{ { 123 456 } } } [
+ H{ { 123 456 } } 1234 [ 100 + ] ?change-of
+] unit-test
+
+{ H{ { 10 2 } } } [
+ H{ { 10 1 } } 10 inc-of
+] unit-test
+
+{ H{ { 10 1001 } } } [
+ H{ { 10 1 } } 10 1000 of+
+] unit-test
+
+{ H{ { 1 100 } } f } [
+ H{ { 1 100 } } 1 100 maybe-set-of
+] unit-test
+
+{ H{ { 1 100 } { 2 100 } } t } [
+ H{ { 1 100 } } 2 100 maybe-set-of
+] unit-test
+
+{ H{ { 1 100 } } t } [
+ H{ { 1 100 } } 1 101 maybe-set-of
+] unit-test
\ No newline at end of file
: ?at ( key assoc -- value/key ? )
2dup at* [ 2nip t ] [ 2drop f ] if ; inline
+: ?of ( assoc key -- value/key ? )
+ swap ?at ; 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
+: maybe-set-of ( assoc key value -- assoc changed? )
+ [ 2dup ?of ] dip swap
+ [ dupd = [ 2drop f ] [ set-of t ] if ] [ nip set-of t ] if ;
+
<PRIVATE
: assoc-operator ( assoc quot -- alist quot' )
: at ( key assoc -- value/f )
at* drop ; inline
-: ?of ( assoc key -- value/key ? )
- swap ?at ; inline
-
: of ( assoc key -- value/f )
swap at ; inline
swap [ nip key? ] curry assoc-filter ;
: assoc-union! ( assoc1 assoc2 -- assoc1 )
- over [ set-at ] with-assoc assoc-each ;
+ [ set-of ] assoc-each ; inline
: assoc-union-as ( assoc1 assoc2 exemplar -- union )
[ [ [ assoc-size ] bi@ + ] dip new-assoc ] 2keepd
: ?change-at ( ..a key assoc quot: ( ..a value -- ..b newvalue ) -- ..b )
2over [ set-at ] 2curry compose [ at* ] dip [ drop ] if ; inline
+: change-of ( ..a assoc key quot: ( ..a value -- ..b newvalue ) -- ..b assoc )
+ [ [ of ] dip call ] 2keepd rot set-of ; inline
+
+: ?change-of ( ..a assoc key quot: ( ..a value -- ..b newvalue ) -- ..b assoc )
+ [ set-of ] compose [ 2dup ?of ] dip [ 2drop ] if ; inline
+
: at+ ( n key assoc -- ) [ 0 or + ] change-at ; inline
: inc-at ( key assoc -- ) [ 1 ] 2dip at+ ; inline
+: of+ ( assoc key n -- assoc ) '[ 0 or _ + ] change-of ; inline
+
+: inc-of ( assoc key -- assoc ) 1 of+ ; inline
+
: map>assoc ( ... seq quot: ( ... elt -- ... key value ) exemplar -- ... assoc )
dup sequence? [
[ [ 2array ] compose ] dip map-as