Associative mapping protocol
Factor handbook » The language » Collections » Associative mapping operations

Next:Lookup and querying of assocs


All associative mappings must be instances of a mixin class:
assoc

assoc? ( object -- ? )


All associative mappings must implement methods on the following generic words:
at* ( key assoc -- value/f ? )

assoc-size ( assoc -- n )

>alist ( assoc -- newassoc )


Mutable assocs should implement the following additional words:
set-at ( value key assoc -- )

delete-at ( key assoc -- )

clear-assoc ( assoc -- )


The following three words are optional:
value-at* ( value assoc -- key/f ? )

new-assoc ( capacity exemplar -- newassoc )

assoc-like ( assoc exemplar -- newassoc )


Assocs should also implement methods on the clone, equal? and hashcode* generic words. Two utility words will help with the implementation of the last two:
assoc= ( assoc1 assoc2 -- ? )

assoc-hashcode ( n assoc -- code )


Finally, assoc classes should define a word for converting other types of assocs; conventionally, such words are named >class where class is the class name. Such a word can be implemented using a utility:
assoc-clone-like ( assoc exemplar -- newassoc )