Hi Doug,

I don't think it's usually a good idea to use any-type! arguments for
functions, since that can easily lead to bugs.

>> vword [a "APPLE" b "BANANA" c "CARROT"] 'c print "oops"
oops
== "CARROT"

Programmers less experienced than you might wonder why the statements
get carried out in the "wrong" order".

How about this:

vword: func [
  { Test to see if a certain word is found in a block or object.
    If so, the value of the word is returned. (Hence, the name vword.)
    If not, then the alternate value will be used.
    If the word is not found and no alternate value is supplied, none is
    returned.}
  item [block! object!] {The item containing the word you wish to search for.}
  i-word [word! block!] {The word / attribute you wish to search for.
        An alternate value may be specified as the second item of a block.}
  /local ret i-alt
][
  if block? i-word [
        i-alt: pick i-word 2
        i-word: first i-word
  ]
  either block? item [
     either none? ret: select item :i-word  [ i-alt ][ ret ]
  ][
    either none? ret: in item :i-word [ i-alt ][ get in item :i-word ]
  ]
]

>> vword [a "APPLE" b "BANANA" c "CARROT"] [d "DICED PEACHES"]
== "DICED PEACHES"
>> vword [a "APPLE" b "BANANA" c "CARROT"] [a "DICED PEACHES"]
== "APPLE"
>> vword [a "APPLE" b "BANANA" c "CARROT"] 'e
== none
>> vword [a "APPLE" b "BANANA" c "CARROT"] [e]
== none
>> xo: make object! [a: "ABC" b: "BOY"]
>> vword xo [a "alpha"]
== "ABC"
>> vword xo [c "alpha"]
== "alpha"
>> vword xo [c]
== none
>> vword xo 'c
== none

This also works:

>> vword [1 "APPLE" 2 "BANANA" 3 "CARROT"] [2]
== "BANANA"
>> vword [1 "APPLE" 2 "BANANA" 3 "CARROT"] [4 "DICED PEACHES"]
== "DICED PEACHES"

Eric

>For your consideration:
>
>We are using a function called "vword" as
>(value of word in block or object)
>a safe, easy way to get unknown values out of blocks or objects.
>
>For instance:
>   We store email/report templates in blocks, but can now safely skip
>   any report details by leaving the items out of the template/blocks or
>   providing a default value to use as a substitute.
>
>  Another great use for "vword" is when decoding CGI gets or posts.
>  You never really know what is in or out of CGI object,
>  so vword is a safe way to interogate the object and get all the values.
>
>
>Try it out and let me know what you think.
>
>REBOL []
>;---------------------------------------------------------------------------
>---------
>vword: func [
>  { Test to see if a certain word is found in a block or object.
>    If so, the value of the word is returned. (Hence, the name vword.)
>    If not, then the alternate value will be used.
>    If the word is not found and no alternate value is supplied, none is
>returned.}
>  item [block! object!] "The item containing the word you wish to search for."
>  i-word [word!]        "The word / attribute you wish to search for."
>  i-alt  [any-type!]    "The alternate value to use if the word is not found."
>  /local ret
>][
>  either block? item [
>     if none? ret: select item :i-word  [
>        if (value? 'i-alt) [ret: i-alt]
>     ]
>  ][
>    either none? ret: in item :i-word [
>       if (value? 'i-alt) [ret: i-alt]
>    ][
>       ret: get in item :i-word
>    ]
>  ]
>  return ret
>]
>;---------------------------------------------------------------------------
>---------
>
>
>
>>> do %/e/scripts/rs/vword.r
>Script: "Untitled" (none)
>>> vword [a "APPLE" b "BANANA" c "CARROT"] 'd "DICED PEACHES"
>== "DICED PEACHES"
>>> vword [a "APPLE" b "BANANA" c "CARROT"] 'a "DICED PEACHES"
>== "APPLE"
>>> vword [a "APPLE" b "BANANA" c "CARROT"] 'b "DICED PEACHES"
>== "BANANA"
>>> vword [a "APPLE" b "BANANA" c "CARROT"] 'c "DICED PEACHES"
>== "CARROT"
>>> vword [a "APPLE" b "BANANA" c "CARROT"] 'e "DICED PEACHES"
>== "DICED PEACHES"
>>> vword [a "APPLE" b "BANANA" c "CARROT"] 'e
>== none
>
>
>
>
>>> print mold xo
>
>make object! [
>    a: "ABC"
>    b: "BOY"
>]
>>> vword xo 'a "alpha"
>== "ABC"
>>> vword xo 'c "alpha"
>== "alpha"
>>> vword xo 'c
>== none
>>> vword xo 'c "alpha"
>== "alpha"
>>> vword xo 'b "alpha"
>== "BOY"
>>>

Reply via email to