Rebol: Found a way to auto-generate and execute code dynamically, is there a better way?

StackOverflow https://stackoverflow.com/questions/1437110

  •  08-07-2019
  •  | 
  •  

Question

I have experimented with this:

>> code-block: copy []
== []
>> append code-block [func[][print "a"] ]
== [func [] [print "a"]]
>> do do code-block
a
>>

Is there a way to avoid to do "do" twice :)

Was it helpful?

Solution

What you have put into code-block is not the function, but the source of the function, hence the need to do it once to make a function, then do it again as a function.

You can see that like this:

length? code-block
== 3

To just put the function in code-block, can do this:

code-block: copy []
append code-block func[][print "a"]    ;; no block around the FUNC

Or this:

code-block: copy []
append code-block reduce [func[][print "a"] ]  ;; use REDUCE to evaluate the block

Either way, what is in code-block is now just the function:

length? code-block
== 1
type? first code-block
== function!
do code-block     ;; what you asked for!
a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top