Task

The goal is to create an associative array (also known as a dictionary, map, or hash).



Related tasks:

 Associative arrays/Iteration
 Hash from two arrays


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


: dip  swap '_ set execute _ ; : nip  swap drop ;
: first  0 extract nip ; : second  1 extract nip ;
 
: assoc-in  swap keys eq ;
: assoc-index'  over keys swap eq [1] index collapse ;
: at  swap assoc-index' subscript collapse second ;
: delete-at  swap assoc-index' first remove ;
: keys  1 transpose first ;
: set-at
    over 'dup dip assoc-in '+ reduce if 'dup dip delete-at then
    "swap 2 compress 1 compress" dip swap append ;
 
[['foo 5]]
10 'bar rot set-at
'bar over at .
'hello 'bar rot set-at
20 'baz rot set-at .