Carl Sassenrath, CTO
REBOL Technologies
24-Apr-2009 14:56 GMT

Article #0193
Main page || Index || Prior Article [0192] || Next Article [0194] || 9 Comments || Send feedback

Moving forward with finishing a few more of the required native functions, collect-words has been added. It exports the internal mechanism used for building contexts, such as objects, modules, and functions.

The main advantage of collect-words function is that it is linear, not N squared where N is the number of words, nor does it require hashing overhead. The secondary advantage is that word collection is generally useful in REBOL, so you don't need to write your own now.

        COLLECT-WORDS block /deep /set /ignore words

        Collect unique words used in a block (used for context construction).
        COLLECT-WORDS is a native value.

        block (block!)

        /deep -- Include nested blocks
        /set -- Only include set-words
        /ignore -- Ignore prior words
                words -- Words to ignore (object! block!)

For example, the funct function needs to collect all set words and make the local variables, but avoid collecting the functions argument variables. Its code becomes:

funct: func [
    {Defines a user function assuming all set-words are locals.}
    spec [block!] {Help string (opt) followed by arg words (and opt type and string)}
    body [block!] {The body block of the function}
    spec: copy/deep spec
    body: copy/deep body
    unless find spec /local [append spec /local]
    append spec collect-words/deep/set/ignore body spec
    make function! reduce [spec body]


Updated 12-Jun-2024 - Edit - Copyright REBOL Technologies -