r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[!REBOL3-OLD1]

Ladislav
5-Apr-2006
[51x2]
I hope it is OK to post here a "technical question"?
some of you know my http://www.fm.tul.cz/~ladislav/rebol/lfunc.r
script defining a "luxury function" with initialization of "static 
variables". Example of the behaviour:

    f: lfunc [] [a: 0] [a: a + 1]
    f ; == 1
    f ; == 2


; etc. The behaviour is caused by the fact, that the initialization 
block causes all variables in it to become "static local variables", 
i.e. in this case A is static and local. It looked "natural" for 
me to define it this way.
Pekr
5-Apr-2006
[53]
and the question is?
Ladislav
5-Apr-2006
[54]
Rebol3 is going to have CLOSURE-type functions for which initialization 
may be "cheap". Example:

    c: closure [/local a] [a: 0] [a: a + 1]

The difference lies in the fact, that the behaviour would be:

    c ; == 1
    c ; == 1


, i.e. the value will be reset to initial value every time a closure 
is called.
Pekr
5-Apr-2006
[55]
ah, sorry to interrupt you ...
Ladislav
5-Apr-2006
[56x3]
Would you like to have this kind of initialization?
(because I may imagine some people saying this is "unexpected" to 
them, or expected, ...)
e.g. you, Pekr, would you like to have this, don't like, don't mind, 
any other option...?
Geomol
5-Apr-2006
[59]
How would a function be defined with the same functionality as your 
lfunc?
Ladislav
5-Apr-2006
[60]
it is quite easy to define as a mezzanine, you may either have a 
look at the implementation mentioned above or ask me to post somewhere 
a simplified version (because the one mentioned above is probably 
more complicated than you would like...). Disadvantages of my LFUNC 
are, that it needs one special "static" context created when the 
function is being defined, which costs some time, e.g.
Geomol
5-Apr-2006
[61]
So your lfunc will still work in REBOL3, as it does now?
Ladislav
5-Apr-2006
[62]
I am pretty sure it will, at worst with a simple modification
Geomol
5-Apr-2006
[63x2]
To understand it better...
I can make a function this way to have initialisation every time:
f: func [/local a] [a: 0 a: a + 1]
What exactly is the goal with CLOSURE?
Ladislav
5-Apr-2006
[65x4]
example of the CLOSURE behaviour:
b: []
    f: closure [x] [append b 'x]
    f 1 ; == [x]
    f 2 ; == [x x]
    reduce b ; == [1 2]

; which means, closure variables somehow "remember" their values, 
compare it to:
>> g: func [x] [append a 'x]
>> g 1
== [x]
>> g 2
== [x x]
>> reduce a
== [2 2]
(where a was defined to be an empty block initially)
Geomol
5-Apr-2006
[69]
hm tricky. It needs some getting used to. And you question is, if 
closure should initialise very time? What will happen, if it doesn't?
Ladislav
5-Apr-2006
[70]
CLOSURES are actually something like "more proper" functions, but 
their evaluation is "more expensive" (takes more time)
Geomol
5-Apr-2006
[71x2]
Will people understand the difference?
And is it usefull?
Ladislav
5-Apr-2006
[73x2]
will people understand the difference: *if* they come into trouble 
when using FUNCS, closures may help them to obtain the desired behaviour
more "persuasive" example:
Geomol
5-Apr-2006
[75]
ahh, good point!
Ladislav
5-Apr-2006
[76x4]
make-f-returning: func [x] [does [x]]
f-returning-ok: make-f-returning "ok"
f-returning-ok ; == "ok"
f-returning-wrong: make-f-returning "wrong"
f-returning-wrong ; == "wrong"
f-returning-ok ; == "wrong"
make-g-returning: closure [x] [does [x]]
g-returning-ok: make-g-returning "ok"
g-returning-ok ; == "ok"
g-returning-wrong: make-g-returning "wrong"
g-returning-wrong ; == "wrong"
g-returning-ok ; == "ok"
what do you say to this?
the alternative to the initialization block is to not use it and 
initialize everything to NONE like in the FUNC case
Geomol
5-Apr-2006
[80x2]
ok, I see the difference. And I guess, it's benefitial, because using 
bind can be difficult.

And your question is, if closure should have a third block with "cheap" 
initializations?
Like in your:
 c: closure [/local a] [a: 0] [a: a + 1]
Ladislav
5-Apr-2006
[82]
the question is, if users want to have the additional (actually second) 
block for the initialization purposes of do not mind to have everything 
initialized to NONE
Geomol
5-Apr-2006
[83x3]
Damn hard question! :-)
It mayl be easier to say, when I've done 10 programs using closure. 
But I'll think about it.
may
Ladislav
5-Apr-2006
[86]
you do not have to know anything about closure, you may imagine the 
same question for FUNC case: would you appreciate to be able to specify 
the initial values for locals?
Geomol
5-Apr-2006
[87x3]
Before I answer that..

I use FUNC all the time. I almost never use FUNCTION. I guess, because 
I have to write more with FUNCTION.
I also started to use HAS and DOES, when I found those.
CLOSURE may come in the group with FUNCTION, if it get 3 blocks as 
arguments, so I may not use it.
Could it be possible to make the initial assignments in the first 
(and only) argument block?
Ladislav
5-Apr-2006
[90]
the only disadvantage of initialization I came to is the more complicated 
interface to CLOSURE needing always one more block to specify
Geomol
5-Apr-2006
[91]
I think, that's a very good point! REBOL programmers don't like to 
write much.
Ladislav
5-Apr-2006
[92]
it may be possible to put the initializations into the first block 
but that would cost some syntax complications and differencess too...
Geomol
5-Apr-2006
[93x2]
yeah
Could we have 2 versions of closure, like we have func and function? 
One where init = NONE and one with the extra block?
Ladislav
5-Apr-2006
[95]
probably yes, although Carl always hesitates to include unwanted 
features
Geomol
5-Apr-2006
[96x4]
I can understand that.
Let's see another example with the extra block:
f: closure [x /local a]Ê[a: 2 * pi] [(sine/radians x) / a]

without:
f: closure [x /local a] [a: 2 * pi (sine/radians x) / a]


Is it something like this, you're thinking about? And will the first 
be faster than the second?
And the third possibility (which maybe not is very REBOLish):
f: closure [x /local a: 2 * pi] [(sine/radians x) / a]
Ladislav, will rebcode be part of REBOL3? Do you know?
Ladislav
5-Apr-2006
[100]
I do not know, but think so