[REBOL] Associative data store Re:
From: al:bri:xtra at: 15-Sep-2000 21:22
This is a multi-part message in MIME format.
------=_NextPart_000_04DB_01C01F5B.1A176640
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
I wanted to use integers as keys, so I wrote this up.
[
Rebol [
Title: "Associate"
Name: 'Associate
File: %Associate.r
Author: "Andrew Martin"
Email: [Al--Bri--xtra--co--nz]
Date: 15/September/2000
]
Encapsulate: function [Value] [Block] [
Block: make block! 1 insert/only Block Value Block
]
Associate: function [Block [block!] Key Value] [Index] [
either found? Index: find Block Key [
change/only next Index Encapsulate Value
][
append Block reduce [Key Encapsulate Value]
]
Block
]
Deassociate: function [Block [block!] Key] [Index] [
if found? Index: find Block Key [
remove/part Index 2
]
Block
]
Associated?: function [Block [block!] Key] [Value] [
Value: select Block Key
either none? Value [none] [first Value]
]
]
>> do %Associate.r
>> block: []
== []
>> Associate block 1 "The test is this"
== [1 ["The test is this"]]
>> Associate block 2 [1 2 3]
== [1 ["The test is this"] 2 [[1 2 3]]]
>> associated? block 1
== "The test is this"
>> associated? block 2
== [1 2 3]
>> block/2
== ["The test is this"]
>> deassociate block 2
== [1 ["The test is this"]]
>> deassociate block 1
== []
I don't like 'Encapsulate. Any one know of a better way to do it?
Andrew Martin
ICQ: 26227169
http://members.ncbi.com/AndrewMartin/
http://members.xoom.com/AndrewMartin/
-><-
------=_NextPart_000_04DB_01C01F5B.1A176640
Content-Type: text/x-rebol;
name="Associate.r"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="Associate.r"
[
Rebol [
Title: "Associate"
Name: 'Associate
File: %Associate.r
Author: "Andrew Martin"
Email: [Al--Bri--xtra--co--nz]
Date: 15/September/2000
]
Encapsulate: function [Value] [Block] [
Block: make block! 1 insert/only Block Value Block
]
Associate: function [Block [block!] Key Value] [Index] [
either found? Index: find Block Key [
change/only next Index Encapsulate Value
][
append Block reduce [Key Encapsulate Value]
]
Block
]
Deassociate: function [Block [block!] Key] [Index] [
if found? Index: find Block Key [
remove/part Index 2
]
Block
]
Associated?: function [Block [block!] Key] [Value] [
Value: select Block Key
either none? Value [none] [first Value]
]
]
------=_NextPart_000_04DB_01C01F5B.1A176640--