Greetings,

Thank you for the quick replies to my mailing list questions.  Here is my
attempt at a dictionary/hash table.  It has already gone through one cleanup
after looking at a similar hash table somewhere (??? maybe in one of the
Rebol libraries) so some of the ideas are not mine.

And I'm not a true newbie - I've been playing with Rebol off and on for
several months.  You may be able to tell that my background is Smalltalk.

I would greatfully appreciate any suggestions for:

        Rebol coding style / commenting style
        Rebol coding idioms
        Other approaches/alternatives for hash tables
        Any other suggestions...

Idioms so far:

1.  Give objects a "new" function, that returns "make self".  Initialize
series data in the "new" function, so all instantiations do not get the same
copy.

Many thanks, and best regards,

Stan Silver

; ===========;
; Dictionary ;
; ===========;

dictionary: make object! [
        hash: none

        new: func [] [make self [hash: to-hash []]]
        new-on: func [new-list] [make self [hash: to-hash copy new-list]]

        at: func [key] [select hash key]
        at-put: func [key value /local here] [
                either here: find/tail hash key [
                        change/only here value
                ] [
                        append hash reduce [key value]
                ]
                return value
        ]

        keys: func [] [extract hash 2]
        has-key: func [key] [found? find hash key]
        remove-key: func [key] [remove/part find hash key 2 return self]

        at-or-put: func [key default-value /local value] [
                if value: at key [return value]
                at-put key default-value
        ]
        at-or-do: func [key default-block /local value] [
                if value: at key [return value]
                at-put key do default-block
        ]

        to-block: func [] [make block! hash]
]

; ======;
; Tests ;
; ======;

?: func [result 'word desired] [
        if not (result = desired) [
                print ["TEST FAILED" mold result word mold desired]
        ]
]

d1: dictionary/new-on [one 1 two 2 three 3]

? d1/at 'one                    >> 1
? d1/at-put 'four 4             >> 4
? d1/at 'four                   >> 4
? d1/to-block                   >> [one 1 two 2 three 3 four 4]
? d1/keys                               >> [one two three four]
? d1/has-key 'one               >> true
? d1/has-key 'nine              >> false
? d1/remove-key 'three          >> d1
? d1/to-block                   >> [one 1 two 2 four 4]

d2: dictionary/new

? d2/at-put 'one 1              >> 1
? d2/at 'one                    >> 1

? 'Should                               >> 'Fail

-- 
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.

Reply via email to