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

Prev:Storing keys and values in assocs
Next:Set-theoretic operations on assocs


The following combinators can be used on any associative mapping.

The assoc-find combinator is part of the Associative mapping protocol and must be implemented once for each class of assoc. All other combinators are implemented in terms of this combinator.

The standard functional programming idioms:
assoc-each ( ... assoc quot: ( ... key value -- ... ) -- ... )

assoc-find ( ... assoc quot: ( ... key value -- ... ? ) -- ... key value ? )

assoc-map ( ... assoc quot: ( ... key value -- ... newkey newvalue ) -- ... newassoc )

assoc-map-as ( ... assoc quot: ( ... key value -- ... newkey newvalue ) exemplar -- ... newassoc )

assoc-filter ( ... assoc quot: ( ... key value -- ... ? ) -- ... subassoc )

assoc-filter-as ( ... assoc quot: ( ... key value -- ... ? ) exemplar -- ... subassoc )

assoc-reject ( ... assoc quot: ( ... key value -- ... ? ) -- ... subassoc )

assoc-reject-as ( ... assoc quot: ( ... key value -- ... ? ) exemplar -- ... subassoc )

assoc-partition ( ... assoc quot: ( ... key value -- ... ? ) -- ... true-assoc false-assoc )

assoc-any? ( ... assoc quot: ( ... key value -- ... ? ) -- ... ? )

assoc-all? ( ... assoc quot: ( ... key value -- ... ? ) -- ... ? )


Removing empty keys or values:
sift-keys ( assoc -- assoc' )

sift-values ( assoc -- assoc' )

harvest-keys ( assoc -- assoc' )

harvest-values ( assoc -- assoc' )


Mapping between assocs and sequences:
map>assoc ( ... seq quot: ( ... elt -- ... key value ) exemplar -- ... assoc )

map>alist ( ... seq quot: ( ... elt -- ... key value ) -- ... alist )

assoc>map ( ... assoc quot: ( ... key value -- ... elt ) exemplar -- ... seq )


Destructive combinators:
assoc-filter! ( ... assoc quot: ( ... key value -- ... ? ) -- ... assoc )

assoc-reject! ( ... assoc quot: ( ... key value -- ... ? ) -- ... assoc )

cache ( ... key assoc quot: ( ... key -- ... value ) -- ... value )

2cache ( ... key1 key2 assoc quot: ( ... key1 key2 -- ... value ) -- ... value )