REBOL [
    Title: "Associate"
    Date: 3-Jul-2002/20:40
    Name: Associate
    Version: 1.0.0
    File: %Associate.r
    Author: "Andrew Martin"
    Purpose: "Provides an associative memory store."
    Email: Al.Bri@xtra.co.nz
    Web: http://valley.150m.com
    Category: [db util 4]
]

Associate?: function [
    Association [series! port! bitset! none!]
    Key [any-type!]
    ] [
    Associated
    ] [
    if not none? Association [
        if found? Associated: select/skip Association :Key 2 [
            first Associated
            ]
        ]
    ]

Associate: function [
    Association [series! port! bitset!]
    Key [any-type!]
    Value [any-type!]
    ] [
    Associated
    ] [
    either found? Associated: find/skip Association :Key 2 [
        either none? :Value [
            remove/part Associated 2
            ] [
            change/only next Associated :Value
            ]
        ] [
        if not none? :Value [
            repend Association [Key Value]
            ]
        ]
    Association
    ]

Associate-Many: function [
    Association [series! port! bitset!]
    Key [any-type!]
    Value [any-type!]
    ] [
    Associated
    ] [
    if none? :Value [
        return associate Association :Key :Value
        ]
    either found? Associated: associate? Association :Key [
        append/only Associated :Value
        ] [
        associate Association :Key reduce [:Value]
        ]
    Association
    ]

Keys: func [Association [series! port! bitset!]][
    extract Association 2
    ]