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

Creating functions with local variables

 [1/8] from: robert:muench:robertmuench at: 30-Dec-2002 11:50


Hi, following problem: I have blocks with some code: predicate: [id = 5] action: [myvar: id * 5 print myvar] This code should be executed within an other function like this: foreach [id object] list compose/deep [ if do [(predicate)] [(action)] ] How do I make it that myvar will be a local word? At the moment myvar will be a global word. Next question is how can I create a function from ACTION with local words and specific signature? myfunc: func [id object] action Robert

 [2/8] from: ammon:addept:ws at: 30-Dec-2002 7:28


Hi, Have you tried USE? action: [use [myvar][myvar: id * 5 print myvar]] Didn't test that code... Enjoy!! Ammon Johnson CIO of Addept ---------- (www.addept.ws) 435.616.2322 ---------- (ammon AT addept.ws)

 [3/8] from: robert:muench:robertmuench at: 30-Dec-2002 15:50


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
<<quoted lines omitted: 5>>
> action: [use [myvar][myvar: id * 5 print myvar]] > Didn't test that code...
Hi, I just thought about this one too. I haven't played around with it but IMO this is the way to go. So people that provide plug-in code need to use USE and specify their local variables. Robert

 [4/8] from: anton:lexicon at: 31-Dec-2002 15:55


What about context [myvar: id * 5 print myvar] context should automatically pick up set-words and bind them to the new local context. Anton.

 [5/8] from: robert:muench:robertmuench at: 31-Dec-2002 11:39


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
<<quoted lines omitted: 5>>
> context should automatically pick up set-words > and bind them to the new local context.
Hi, I played around with context too but wasn't able to get it to work as I liked. Here is what I tried: dice2: 3 test: context [ dice2: 2 action-with-var2: [dice2: 2 print ["Dice2:" dice2]] ] ?? dice2 ; >> dice2: 3 myfunct: 'action-with-var2 do test/:myfunct ; >> Dice2: 2 ?? dice2 ; >> dice2: 3 But if you comment one line: dice2: 3 test: context [ ; ### dice2: 2 action-with-var2: [dice2: 2 print ["Dice2:" dice2]] ] ?? dice2 ; >> dice2: 3 myfunct: 'action-with-var2 do test/:myfunct ; >> Dice2: 2 ?? dice2 ; >> dice2: 2 So you still have to mention all local words. And you have to agree on a function that gets called because the context will be an object that doesn't execute any code if used. You have to explicitly call a word from the context. The problem with your approach is: How do you execute it at runtime? It only works if pasted to command line.
>> id: 3
== 3
>> test: context [myvar: id * 5 print myvar]
15
>> test >> do test >> reduce test >> ?? test
test: make object! [ myvar: 15 ] Robert

 [6/8] from: g::santilli::tiscalinet::it at: 31-Dec-2002 15:41


Hi Robert, On Tuesday, December 31, 2002, 11:39:17 AM, you wrote: RMM> So you still have to mention all local words. I imagined you were looking for that; as I said, I think Ladislav has the function for you. Anyway, here's a QAD solution: do-local: func [code [block!] /local words stack block] [ words: clear [] stack: clear [] insert/only tail stack code forall stack [ foreach value pick stack 1 [ if set-word? :value [insert tail words to-word :value] if all [any-block? :value not path? :value] [ insert/only tail stack :value ] ] ] use words code ]
>> do-local [my-var: 3 print my-var]
3
>> my-var
** Script Error: my-var has no value ** Near: my-var
>> do-local [f: func [x] [y: x * x print [x y]] f 3 do-local [y: 5 print y] print y]
3 9 5 9
>> x
** Script Error: x has no value ** Near: x
>> y
** Script Error: y has no value ** Near: y RMM> And you have to agree on a RMM> function that gets called because the context will be an object that RMM> doesn't execute any code if used. You have to explicitly call a word RMM> from the context. No, you have not. You simply use CONTEXT instead of DO... Or, like in the above, you simply use DO-LOCAL instead of DO. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [7/8] from: g:santilli:tiscalinet:it at: 31-Dec-2002 19:17


Replying to myself, GS> use words code Maybe: use unique words code would be better. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [8/8] from: joel:neely:fedex at: 2-Jan-2003 9:56


Hi, Robert, Happy New Year! Interleaved below are a few hints that I hope are relevant (and not too elementary/outdated to be useful to you... ;-) Robert M. Muench wrote:
> > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]] > > On Behalf Of Anton
<<quoted lines omitted: 11>>
> ?? dice2 > ; >> dice2: 3
Since DICE2 appears as a set-word inside the MAKE OBJECT! that word is bound to the new context and (inside ACTION-WITH-VAR2) will be the local one, rather than the global one ...
> But if you comment one line: > dice2: 3
<<quoted lines omitted: 4>>
> ?? dice2 > ; >> dice2: 3
... hence the difference obtained when the set-word is commented out in the object definition.
> So you still have to mention all local words. And you have to agree > on a function that gets called because the context will be an object > that doesn't execute any code if used. You have to explicitly call a > word from the context. > > The problem with your approach is: How do you execute it at runtime? > It only works if pasted to command line. >
Here is a little sample that uses multiple tricks to work around some of the issues that I inferred are in play here: 8<-------- REBOL [] glofun: none do in make object! [ counter: 0 set 'glofun locfun: func [] [counter: counter + 1] configure: func [n [integer!]] [counter: n] ] 'configure to-integer now/time 8<-------- I saved the above in a file named "glopri.r" and then called for it to be evaluated, with the following results:
>> do %glopri.r
Script: "Untitled" (none) == 35292
>> glofun
== 35293
>> glofun
== 35294
>> glofun
== 35295 Hope this helps! -jn-

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted