Here's an implementation for an Associative Data Store (ADS) that uses only
two functions, doesn't require an object per ADS and can use any Rebol value
as a key. Comment invited. Thanks to Gabriele for showing how to shorten the
'Associate? function.

[
Rebol [
    Title: "Associate"
    Name: 'Associate
    File: %Associate.r
    Author: "Andrew Martin"
    Email: [EMAIL PROTECTED]
    Date: 18/September/2000
    ]

Associate: function [Block [block!] Key Value] [Index] [
    Index: find Block Key
    either none? Value [
        if found? Index [
            remove/part Index 2
            ]
        ][
        either found? Index [
            change/only next Index reduce [Value]
            ][
            append Block reduce [Key reduce [Value]]
            ]
        ]
    Block
    ]

Associate?: function [Block [block!] Key] [Value] [
    all [
        Value: select Block Key
        first Value
        ]
    ]
]

Andrew Martin
ICQ: 26227169
http://members.ncbi.com/AndrewMartin/
http://members.xoom.com/AndrewMartin/
-><-

[
Rebol [
	Title: "Associate"
	Name: 'Associate
	File: %Associate.r
	Author: "Andrew Martin"
	Email: [EMAIL PROTECTED]
	Date: 18/September/2000
	]

Associate: function [Block [block!] Key Value] [Index] [
	Index: find Block Key
	either none? Value [
		if found? Index [
			remove/part Index 2
			]
		][
		either found? Index [
			change/only next Index reduce [Value]
			][
			append Block reduce [Key reduce [Value]]
			]
		]
	Block
	]

Associate?: function [Block [block!] Key] [Value] [
	all [
		Value: select Block Key
		first Value
		]
	]

]

Reply via email to