Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: To do or not to do?

From: joel:neely:fedex at: 28-Nov-2001 16:53

Hi, Colin, No guru here, JARH... [Sanghabum--aol--com] 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" ;