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: [EMAIL PROTECTED]
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/
-><-
[
Rebol [
Title: "Association"
Name: 'Association
File: %Association.r
Author: "Andrew Martin"
Email: [EMAIL PROTECTED]
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
]
]
]
[
Rebol [
Title: "Associate"
Name: 'Associate
File: %Associate.r
Author: "Andrew Martin"
Email: [EMAIL PROTECTED]
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
]
]
]