[REBOL] Associative Data Store Re:(6)
From: al:bri:xtra at: 21-Sep-2000 6:17
This is a multi-part message in MIME format.
------=_NextPart_000_00BD_01C02393.AAD19340
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
jn wrote:
> The main difference between our proposals is whether to use a collection
of functions or an object-oriented approach. That discussion gets us into
deeper waters.
In the spirit of putting oil on waters, here's Association.r which allows
the use of objects as well as functions for an Associative Data Store:
[
Rebol [
Title: "Association"
Name: 'Association
File: %Association.r
Author: "Andrew Martin"
Email: [Al--Bri--xtra--co--nz]
Date: 21/September/2000
]
do %Associate.r
Association!: make object! [
Block: block!
Get: func [Key [any-type!]] [
Associate? Block Key
]
Set: func [Key [any-type!] Value [any-type!]] [
Associate Block Key Value
]
]
Association: func [/Size Value [integer!]] [
all [
not Size
Value: 1
]
make Association! [
Block: make block! Value
]
]
]
>> do %Association.r
>> A: Association
>> probe a
make object! [
Block: []
Get: func [Key [any-type!]][
Associate? Block Key
]
Set: func [Key [any-type!] Value [any-type!]][
Associate Block Key Value
]
]
>> a/set [1 2] "hello"
== [[1 2] ["hello"]]
>> a/set [1 5] "1-5"
== [[1 2] ["hello"] [1 5] ["1-5"]]
>> a/get [1 2]
== "hello"
>> a/get [1 5]
== "1-5"
Andrew Martin
Why sleep when I can write Rebol?
ICQ: 26227169
http://members.ncbi.com/andrewmartin/
http://members.xoom.com/AndrewMartin/
-><-
------=_NextPart_000_00BD_01C02393.AAD19340
Content-Type: text/x-rebol;
name="Association.r"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="Association.r"
[
Rebol [
Title: "Association"
Name: 'Association
File: %Association.r
Author: "Andrew Martin"
Email: [Al--Bri--xtra--co--nz]
Date: 21/September/2000
]
do %Associate.r
Association!: make object! [
Block: block!
Get: func [Key [any-type!]] [
Associate? Block Key
]
Set: func [Key [any-type!] Value [any-type!]] [
Associate Block Key Value
]
]
Association: func [/Size Value [integer!]] [
all [
not Size
Value: 1
]
make Association! [
Block: make block! Value
]
]
]
------=_NextPart_000_00BD_01C02393.AAD19340
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: 19/September/2000
]
Associate!: make object! [
Find: function [Block [block!] Key] [Index] [
Index: Block
until [
Index: system/words/find/only Index Key
if not found? Index [return none]
either odd? offset? Block Index [
Index: next Index
false
][
true
]
]
Index
]
set 'Associate? function [Block [block!] Key] [Index] [
either found? Index: Find Block Key [
first second Index
][
none
]
]
set 'Associate function [Block [block!] Key Value] [Index] [
Index: Find Block Key
either none? Value [
if found? Index [
remove/part Index 2
]
][
Value: reduce [Value]
either found? Index [
change/only next Index Value
][
append Block reduce [Key Value]
]
]
Block
]
]
]
------=_NextPart_000_00BD_01C02393.AAD19340--