Hi, Colin,

No guru here, JARH...

[EMAIL PROTECTED] wrote:
> 
> But this little example has got me stumped. So I'm interested
> in how the gurus would unDo my approach.
> 

Let each rule be an anonymous function, applied to the value
being considered.

> 
> ----------------------------------
> ;; rules table
> ;; ===========
>  Rules: [
>         "(Length? to-string *) > 0" "No data"
>         "date? *"                   "Bad date"
>         "(* - 14 ) < now/date"      "Too old"
>         "specialcheck * "           "Not special"
>         ]
> 
> ;; prepare data field
> ;; ==============
> 
>  RawValue: "5-122-2001"      ;; bad date in this example
>  Loadedvalue: ""
>  if error? try [loadedValue: first load/all Rawvalue]
>            [LoadedValue: RawValue]
> 
> ;; apply rules
> ;; ===========
> 
>  foreach [rule message] Rules [
>      if not (do replace/all copy rule "*" mold LoadedValue) [
>        print [LoadedValue " fails rule: " Message]
>               break
>      ] ; if
>  ]  ; for
> ----------------------------------
>

As follows:

    ;; "special" check  ;-)
    ;;
    specialcheck: func [x] [
        random true
    ]

    ;; rules table
    ;; ===========
    Rules: reduce [
        func [x] [0 < Length? to-string x] "No data"
        func [x] [date? x]                 "Bad date"
        func [x] [x - 14 < now/date]       "Too old"
        func [x] [specialcheck x]          "Not special"
    ]

    ;; examine data field
    ;; ==================

    testTheField: function [
        RawValue [string!]
    ][
        LoadedValue
    ][
        if error? try [
            LoadedValue: first load/all RawValue
        ][
            LoadedValue: RawValue
        ]

        ;; apply rules
        ;; ===========

        foreach [rule message] Rules [
            if not (rule LoadedValue) [
                print [LoadedValue " fails rule: " message]
                return false
            ] ; if
        ]  ; for
        true
    ]

which performs as follows:

    >> testTheField "5-122-2001"
    5-122-2001  fails rule:  Bad date
    == false
    >> testTheField "11-01-2001"
    11-Jan-2001  fails rule:  Not special
    == false
    >> testTheField "11-28-2001"
    11-28-2001  fails rule:  Bad date
    == false
    >> testTheField "11-01-2001"
    11-Jan-2001  fails rule:  Not special
    == false
    >> testTheField "11-28-2001"
    11-28-2001  fails rule:  Bad date
    == false
    >> testTheField "11-01-2001"
    == true
    >>

HTH!

-jn-

-- 
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;
-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to