chickadee » integer-map » fxmapping-update

fxmapping-update fxmap k proc #!optional failureprocedure

proc is a procedure of type fixnum * (* → fxmapping) ([] → fxmapping) → * …. If the optional failure parameter is provided, then it must be a procedure of type [] → * ….

Updates the association for k in fxmap as follows. If k has an association in fxmap, then proc is invoked in tail context on k, its associated value, and two procedure arguments, replace and delete, and its values are returned. Invoking replace on a value v returns a fxmapping with the association (k, v) and all of fxmap's other associations. Invoking delete returns a fxmapping with all of the associations of fxmap, but without an association for k. If k has no association in fxmap and failure is provided, then failure is invoked in tail context on no arguments and its values are returned. Otherwise, it is an error.

Note that, in contrast to similar Scheme forms, proc is not required to tail-call one of delete or replace, and that fxmapping-update simply returns the results of invoking proc (assuming k is found). Thus, the following is valid:

(fxmapping-update (fxmapping 0 'a 1 'b 2 'c)
                  1
                  (lambda (k v replace _del)
                    (values k v (fxmapping->alist (replace 'z)))))
 ⇒ 1
   b
   ((0 . a) (1 . z) (2 . c))

Simple versions of several other update operations may be defined in terms of fxmapping-update, e.g.:

(fxmapping-delete fxmap k)
 ≡
(fxmapping-update fxmap k (lambda (_k _v _r delete) (delete)))

(fxmapping-set fxmap k v)
 ≡
(fxmapping-update fxmap k (lambda (_k _u replace _d) (replace v)))

Examples:

;; Delete the association for 1 if its value is a symbol.
(fxmapping->alist
 (fxmapping-update (fxmapping 0 'a 1 'b 2 'c)
                   1
                   (lambda (_k v replace delete)
                     (if (symbol? v)
                         (delete)
                         (replace v)))))
 ⇒ ((0 . a) (2 . c))

(fxmapping->alist
 (fxmapping-update (fxmapping 0 'a 1 'b 2 'c)
                   1
                   (lambda (k _v replace _d)
                     (replace k)))))
 ⇒ ((0 . a) (1 . 1) (2 . c))