Refining my understanding of words
Let's say I want to so something with the month, day, and year parts of a date. In the sample below, in case 1, I obtain those parts by using the "now" function three times. That could involve three calls to the operating system, which might have a bit of overhead. In case 2, if this were a non-REBOL language, TODAYS-DATE would be a "variable" and would "contains" the result of the "now" function, and references to TODAYS-DATE/MM2, DD2, and YY2 would NOT involve calls to the operating system, and only the line that loaded TODAYS-DATE would call the operating system. BUT, in REBOL where words refer to values, do the three lines that refer to TODAYS-DATE STILL call the "now" function three times because TODAYS-DATE only REFERS to "now," and the "now" function must be executed every time I use the word TODAYS-DATE? Or does case 2 operate like I hope it does, where the "now" function is executed only once? R E B O L [ Title: "Test harness" ] ;; Case 1 MM1: now/month DD1: now/day YY1: now/year print [MM1 "/" DD1 "/" YY1] ;; Case 2 TODAYS-DATE: now MM2: TODAYS-DATE/month DD2: TODAYS-DATE/day YY2: TODAYS-DATE/year print [MM2 "/" DD2 "/" YY2] halt
posted by: Steven White 15-Sep-2017/15:59:39-7:00
It's a theory you could test by introducing a delay via WAIT and seeing whether it affected the times... When the evaluator sees a WORD! that is bound to a FUNCTION!...and this word is not in a "quoted slot"...the function will be called. When the evaluator sees a SET-WORD!, it will evaluate the expression on its right and then store the value into the bound location of that SET-WORD!. NOW is a FUNCTION!. `TODAYS-DATE: now` stores the result of calling the NOW function into the TODAYS-DATE variable. If the result of NOW was itself a FUNCTION!, then TODAYS-DATE would then be a function...and you'd wind up calling it when you mentioned it as a word or path. But NOW returns a DATE!, and it is captured at a moment in time and then you are extracting the properties of that one capture--as you hope.
posted by: Fork 15-Sep-2017/17:34:23-7:00
x: now/precise ; the same value going forward y: does [now/precise] ; a different value each time print reduce [x y wait 1 x y]
posted by: Nick 15-Sep-2017/20:46:13-7:00
Steven White, todays-date: :now Is what you thought I guess.
posted by: Endo 8-Oct-2017/8:54:42-7:00
Endo, of course, don't know why I didn't think to clarify that. To be clear: x: now ; the same value going forward y: :now ; a different value each time
posted by: Nick 8-Oct-2017/9:13:41-7:00
|