Hi Joel,
you wrote:
>[EMAIL PROTECTED] wrote:
>>
>> ... Just wondering how performance will be. :)
>>
>
>That was the motivation for using paired blocks rather than interleaving
>the keys and values in a single block.
>If I had used
>interleaved keys and values, get would have to be written something
>like the following:
>
>;!!! WARNING: UNTESTED CODE FRAGMENT !!!
>
> assoc: make object! [
> _kvpairs: copy []
>;
>; etc...
>;
> get: func [t [string!] / local k v] [
> foreach [k v] _kvpairs [
> if k = t [return v]
> ]
> none
> ]
If your only concern is avoiding loops, and that is why you are not using a
single block to contain both keys and values, then why not use a single
block in combination with the path notation?
assoc: make object! [
_kvpairs: copy []
get: func [t [string!] /local path] [
path: make path! compose [_kvpairs (t)]
return path
]
That should do it. You can do the whole thing with paths, without supplying
any new functions or objects:
>> phonelist: make block! 10
== []
>> insert phonelist [Jim "555-3333"]
== []
>> insert phonelist [Dick "555-2222"]
== [Jim "555-3333"]
>> insert phonelist [Judy "555-4444" Jane "555-5555"]
== [Dick "555-2222" Jim "555-3333"]
>> insert phonelist [Bill "555-1111"]
== [Judy "555-4444" Jane "555-5555" Dick "555-2222" Jim "555-3333"]
>> insert phonelist [Al "555-9999"]
== [Bill "555-1111" Judy "555-4444" Jane "555-5555" Dick "555-2222" Jim
"555-3333"]
>> phonelist/Bill
== "555-1111"
>> phonelist/Dick
== "555-2222"
>> phonelist/Jim
== "555-3333"
>> phonelist/Judy
== "555-4444"
>> phonelist/Al
== "555-9999"
>> phonelist/Jane
== "555-5555"
>> foreach [who phone] sort/skip copy phonelist 2 [
[ print [who phone]
[ ]
Al 555-9999
Bill 555-1111
Dick 555-2222
Jane 555-5555
Jim 555-3333
Judy 555-4444
>;
>; etc...
>;
> ]
>
>which puts real work (proportional to the size of the store) up into
>interpreted code. That's why I avoided it.
>
>>
>> I still want something like this built into the language. Also, that way we
>> could use built ins like 'length? on it.
>>
>> Hmm... this brings up another question. There's no way to do any kind of
>> operator overloading in Rebol, so that we *could* use 'length? on this data
>> structure, is there?
>>
>
>Your wish is my command! (At least, this once! ;-)
>
>================================================================
>REBOL [
> Title: "Minimal Associative Data Store"
> File: %assoc.r
> Date: 14-Sep-2000
> Author: "Joel Neely"
> Purpose: {
> Quick, minimal implementation of a data store that
> associates arbitrary values with (string!) keys.
> }
>]
>
>assoc: make object! [
> _keys: copy []
> _vals: copy []
> clear: func [] [_keys: copy [] _vals: copy []]
> empty?: func [] [system/words/empty? head _keys]
> length?: func [] [system/words/length? _keys]
> new: func [/local r] [r: make assoc [] r/clear r]
> put: func [k [string!] v [any-type!] /local p] [
> either none? p: find _keys k [
> append _keys k
> append _vals v
> ][
> change at _vals index? p v
> ]
> v
> ]
> get: func [k [string!] /local p] [
> either none? p: find _keys k [
> none
> ][
> pick _vals index? p
> ]
> ]
> delete: func [k [string!] /local p v] [
> either none? p: find _keys k [
> none
> ][
> k: pick _vals p: index? p
> remove at _keys p
> remove at _vals p
> k
> ]
> ]
> keys: func [] [copy _keys]
>]
>================================================================
>
>-jn-
>
>--
>; Joel Neely [EMAIL PROTECTED] 901-263-4460 38017/HKA/9677
>REBOL [] print to-string debase/64 decompress #{
> 789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC
> B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}
>
>
>
;- Elan [ : - ) ]
author of REBOL: THE OFFICIAL GUIDE
REBOL Press: The Official Source for REBOL Books
http://www.REBOLpress.com
visit me at http://www.TechScribe.com