• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[Rebol School] REBOL School

BrianH
14-Aug-2012
[828x3]
If you are doing anything in PARSE, or generating blocks, or a wide 
variety of other things, it almost always pays off to do it in R3 
if you can, rather than R2 or any of the other REBOL-alikes except 
maybe Topaz. Topaz has a few PARSE enhancements that R3 could use, 
but the rest don't even come close; there are so many bugs in R2 
that need to be worked around that code for it can be quite different. 
Unfortunately, other factors can prevent people from using R3. Hopefully 
when Red itself is developed, not just Red/System, it will adopt 
the changes that we made for R3.
I'd love to see the Topaz PARSE enhancements in Red too :)
Gabriele has done a great job of reimplementing many of R3's PARSE 
enhancements in R2 using rule generators. If you're stuck on R2 or 
REBOL-alikes with R2-like behavior, that's a good place to start. 
It also helps to look at the parse project page, since there are 
a lot of code equivalent examples in the proposals there. That way 
you can translate your rules manually, and in some cases get faster 
rules as a result. Be careful with unset! vlaues though since there 
are a lot of bugs and limitations to R2's unset value handling, as 
demonstrated in the R2 RLE code above.
james_nak
14-Aug-2012
[831]
Brian, it's so nice to have a Parse expert here. Thanks for your 
advice.
BrianH
14-Aug-2012
[832]
Just for fun :)
james_nak
14-Aug-2012
[833x2]
It's no fun when you can' t figure out why something you think should 
work doesn't :-)
Really though, you are probably one of the few who really know Parse.
Marco
14-Aug-2012
[835]
Speaking of Parse. I think it could be useful to compile some kind 
of FAQ or even better a tips&tricks doc.
Gabriele
15-Aug-2012
[836]
Graham, wait, are you using MOLD on the output? The surrounding { 
} are as much in there as the surrounding ' ' in the JS output.
GrahamC
15-Aug-2012
[837]
Nope ... no mold
GrahamC
16-Aug-2012
[838x2]
Amazon SWF holds the connection open for 60 seconds to allow events 
to appear.  This times out my http(s) connections though.  I tried 
setting system/schemes/timeout to 60 but that doesn't work??
system/schemes/default/timeout
DocKimbel
16-Aug-2012
[840x2]
Tried setting system/schemes/http/timeout too?
or rather system/schemses/https/timeout
GrahamC
16-Aug-2012
[842]
Yes
Maxim
16-Aug-2012
[843]
put it higher than 60 seconds?...  we have tcp connections open for 
45 minutes so I don't think  REBOL is at fault ...  note that you 
have to set the default timeouts before you start opening ports (I 
assume you did this ;-).
GrahamC
16-Aug-2012
[844]
just using the default http scheme ....
MaxV
27-Aug-2012
[845]
What odes this function: http://synapse-ehr.com/community/threads/what-does-this-function-do.1538/
 ?
BrianH
27-Aug-2012
[846]
Two things:

- It iterates through a range of numbers by step, sort of like FOR, 
returning the new value.

- It saves the state of these iterations in a block that is local 
to the function, which it calls "stack" even though it is not a stack.

Here's the function commented:

iter: func [idx start end step  /local stack][

 stack: head []  ; Saved iteration states, persistently updated, indexed 
 by idx
	; Prefill with none through idx
	while [tail? stack: at head stack idx] [insert tail stack none]
	; Initialize iteration state to end value
	if stack/1 = none [stack/1: end ]
	; Increment by step
	stack/1: stack/1 + step
	; If we've gone past the end, rotate back to the start
	if stack/1 > end [stack/1: start]
	; Return the current value of the iteration
	stack/1
]

And optimized/simplified:

iter: func [idx start end step /local stack][

 stack: []  ; head unnecessary, the inline value is at its head already

 insert/dup tail stack none idx - length? stack  ; faster than while

 stack: at stack idx  ; will succeed now, position doesn't persist 
 between calls

 stack/1: step + any [stack/1 end]  ; initialize and increment in 
 one step
	if stack/1 > end [stack/1: start]
	stack/1
]


Note that the function doesn't work if idx, start and end aren't 
numbers, start isn't less than end, and step isn't greater than 1. 
However, all of the code that calls it has these qualities.
Arnold
27-Aug-2012
[847]
Hi, I would have used stack: copy []  but this is not necessary here? 
Because stack is local?
Gregg
27-Aug-2012
[848]
If you use COPY it won't persist between function calls.
Arnold
27-Aug-2012
[849]
It is stil confusing (to me) because the variable 'stack' is declared 
to be a local variable.
Ladislav
27-Aug-2012
[850x3]
stack: []  ; head unnecessary, the inline value is at its head already
 - are you sure?
aha, yes, it is correct
It is stil confusing (to me) because the variable 'stack' is declared 
to be a local variable.
 - more confusing is that it is not a stack as Brian pointed out
GrahamC
27-Aug-2012
[853x2]
Arnold, 'stack is local to the function.  Ordinarily you would make 
a copy each time the function is called so that you have an empty 
new block.  But here the function does not so that the block contents 
are available unchanged between calls.
So, I guess my question is .. when do local variables get garbage 
collected?
Ladislav
27-Aug-2012
[855x3]
When do local variables get garbage collected?
 - never, only in case they are not accessible
,i.e. only in case the function is collected
however, the STACK local variable is actually not just "local variable", 
it is a value in the function body. In this case the only way how 
the value could be collected is to collect the function
GrahamC
27-Aug-2012
[858]
so you would have to unset the function ?  Or just set it to none?
Ladislav
27-Aug-2012
[859x2]
However, the function looks strange to me. I guess that it should 
have been implemented differently.
so you would have to unset the function ?

 - you cannot unset a function. You can only unset the variable referring 
 to it.
GrahamC
27-Aug-2012
[861]
yes, that's what I meant
Maxim
27-Aug-2012
[862]
Arnold, if you are proficient at language C,  R2's local word model 
is the same as static variables in C.
Ladislav
27-Aug-2012
[863]
E.g.

    f: func [] []
    unset 'f

would make the function inaccessible (collectable), but, e.g.

    g: func [][]
    g: 1

makes the function inaccessible (collectable) as well
Maxim
27-Aug-2012
[864]
that wasn't worded how I meant it...
Ladislav
27-Aug-2012
[865]
R2's local word model is the same as static variables in C.
 - hmm, there are some significant differences
Maxim
27-Aug-2012
[866x3]
Arnold, What I meant to say (and what ladislav is trying to say, 
I think) is that the code inside a function is not Garbage collected, 
so when a function has a litteral value.. that litteral value is 
the same at each execution (its static).
the local values are reset to none at each execution, but they will 
be re-assigned to the same litteral.
(which is why they look like static variables in terms of series 
which are references)
GrahamC
27-Aug-2012
[869]
So, anonymous functions are automatically garbage collected?
Maxim
27-Aug-2012
[870x2]
no.  it depends if they are referenced at least once in a value which 
is not also GCd.
so if you have a function in a block, named or not, it won't be GCd 
if that block is still refered to somewhere.
GrahamC
27-Aug-2012
[872]
so let's say you have something in a vid defintion

button "Test" [
	use [ f ][
		f: func [] []
	]
]

this is not subject to GC?
Ladislav
27-Aug-2012
[873x2]
the local values are reset to none at each execution
 - that should have been:


the local variables are reset to NONE at every function call. After 
the function returns, the local variables are:
- in R2 they 
remember" the last (outermost call) value
- in R3 their values are inaccessible
this is not subject to GC?

 - Such a function looks like a subject to GC, but you need to find 
 out *when*.


- in R2 it is a subject to GC right after the button is pressed and 
released

- in R3 the function *may* persist after the button is pressed for 
the first time and *before* it is pressed for the second time
GrahamC
27-Aug-2012
[875]
@Ladislav, have you written a paper on GC in Rebol ?
Ladislav
27-Aug-2012
[876x2]
Aha, I was wrong with R2, actually in R2 the function persists between 
presses as well, but the reasons are different for R2 and R3
have you written a paper on GC in Rebol ?

 - the reason why the function persists between presses in R2 is based 
 on the fact that USE modifies its argument block "leaving" the local 
 variable in it. (it is described in Bindology). In R3 USE is a closure 
 written in REBOL, that is why there may be some "persistence" until 
 the USE function is called next time