Here's another version of %Associate.r that should cope with the tests that
Ladislav and Joel have thrown at it.

I haven't yet worked out how to fix Ladislav's concern yet:

> The other problem is, that your functions won't accept Any-type! values
for Key/Value

Comments and criticism gratefully accepted.

I'm sure that my wrapper around 'Find can be done better, too.

[
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
        ]
    ]

]
>> do %associate.r
>> blk: []
== []
>> Associate blk [2 3] 5
== [[2 3] [5]]
>> Associate? blk [2 3]
== 5
>> Associate blk 5 7
== [[2 3] [5] 5 [7]]
>> Associate blk [5] 9
== [[2 3] [5] 5 [7] [5] [9]]
>> Associate? blk 5
== 7
>> Associate? blk [5]
== 9
>> Associate? blk reduce [2 3]
== 5
>> Associate? blk reduce [[2 3]]
== none
>> Associate? blk reduce [[5]]
== none
>> blk: copy []
== []
>> associate blk [2] 3
== [[2] [3]]
>> associate? blk [2]
== 3

Andrew Martin
Nietzchean Rebolutionary...
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: 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
		]
	]

]

Reply via email to