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

World: r3wp

[Core] Discuss core issues

Geomol
31-Mar-2009
[13307]
Yes, I read that discussion again yesterday. I remember, that I also 
didn't see the great use of it back then. :-)
Henrik
31-Mar-2009
[13308]
I guess you've not bumped into that knot.
Geomol
31-Mar-2009
[13309]
Maybe if I see a bit larger program, that use ALSO!?
Henrik
31-Mar-2009
[13310]
anway, I would be sad to see it go, so I want it to stay.
Geomol
31-Mar-2009
[13311x2]
I learned a program structure more than 20 years ago called "program 
95%". It's a structure, you use 95% of the time when programming 
COBOL (and probably also in most other langauges). Maybe the need 
for ALSO is related to how we structure our programs?
The structure is basically:
init get-input loop [handle input get-input until end] cleanup
Henrik
31-Mar-2009
[13313x2]
I'm not sure it is. For the port example above, there's no way to 
return without ALSO or assigning a temporary variable. If you are 
using it inside another function, like process-port, it will only 
reduce overhead, not create more.
and you really want functions like that to be simple.
Geomol
31-Mar-2009
[13315]
What is the overhead for calling a function compared to not call 
it? And by having the close down in a function mean, it may not be 
on the same level as the open, which may be seen as bad programming 
style.
Henrik
31-Mar-2009
[13316]
as said, if you use the function in 50 places...
Geomol
31-Mar-2009
[13317]
So you have
port: open .....
my-port: process-port port

many places, where I suggest:

port: open ...
my-port: copy port
close port
Henrik
31-Mar-2009
[13318]
no, you may exactly _not_ have 'my-port. you may be doing a test 
on the port which does not require an extra word.
Geomol
31-Mar-2009
[13319x2]
I would never do the first, because there is no close to be seen 
for every open.
So you don't really need to copy the port? It's just for a test, 
like:

port: open ....
while [port] [
....
]
close port
Henrik
31-Mar-2009
[13321x2]
sorry, the content.
I give up. :-) gotta get back to coding.
Geomol
31-Mar-2009
[13323]
:-) I need to see bigger examples to understand you guys.
Henrik
31-Mar-2009
[13324]
ask Carl on Chat. I think that's best. It was his idea anyway.
Oldes
31-Mar-2009
[13325]
Geomol: I wonder why you have a problem with 'also now. It was discussed 
on 10-Aug-2007 on r3-alpha in new-functions group - and you were 
one of the people who were in this discussion
Geomol
31-Mar-2009
[13326]
Yes, I then said, I didn't see the need for it, but I suggested a 
good name. Now 1.5 years later, I still don't use it. I'm asking, 
if people use it, because if I see good use of it, I hope to use 
it myself.
Oldes
31-Mar-2009
[13327]
I'm not using it.. I'm not coding in R3 yet. Also when I test 'also 
now, it looks that there is no speed gain.
Izkata
31-Mar-2009
[13328x2]
Huh..  I didn't know there was an 'also - I defined my own function 
long ago that does what it does
I've used it in a couple places, almost always having to do with 
ports - it is much nicer than spanning 3 lines for the same thing
Geomol
31-Mar-2009
[13330]
Do you have any code, I can see? To see how you use it.
[unknown: 5]
31-Mar-2009
[13331x2]
John, consider if you have a local variable in a function.  Then 
calling also can clear that local variable.  This is what I use 'ALSO 
for the most and why it is way cool.
It is way cool when the item your returning is the very item you 
want to clear.
Geomol
31-Mar-2009
[13333]
And why do you want to clear it? To save resources? But what then 
with the version, you return?
[unknown: 5]
31-Mar-2009
[13334]
Yes to save resources.  For example, what if I just read an MP3 file 
into a word? I want to free that word so that it no longer is allocated 
to that data.
Geomol
31-Mar-2009
[13335]
If you want to load more MP3s, it would be a good idea to reuse the 
same memory area. An example:

>> f: func [/local blk] [blk: []]   
>> a: f
== []
>> insert a 42
== []
>> a
== [42]
>> source f
f: func [/local blk][blk: [42]]
>> clear a
== []
>> source f
f: func [/local blk][blk: []]


You see, A and BLK inside F points to the same memory. Next time 
you call F, you can put some more stuff in BLK (read another MP3).

If you want to be able to completely remove BLK, I would do that 
using an object. Build the MP3 player as an object, instead of just 
a function. Makes sense?
[unknown: 5]
31-Mar-2009
[13336x3]
Sure, John, but what if you don't want to load more mp3s?
Of if between loading mp3s you have other functions that are operating 
and you want to ensure that your keeping memory use at optimal levels?
I see no alternative to having the functionality of ALSO in those 
cases.
Geomol
31-Mar-2009
[13339]
You still have to set the MP3 data returned from your function to 
none to release the memory (making the garbage collector to work). 
I would handle this situation either by making a refinement to my 
function or (probably better) creating the whole thing as an object.
[unknown: 5]
31-Mar-2009
[13340x2]
that is what we do with ALSO, we set the return item to none.
also mp3data mp3data: none
Geomol
31-Mar-2009
[13342x2]
f: func [/clear /local blk] [
	either clear [
		blk: none
	] [
		blk: []
		comment "do something with blk"
		 return blk
	]
]
But your memory isn't cleared by using ALSO! You still get something 
back from your function. You have to clear this later in your code.
[unknown: 5]
31-Mar-2009
[13344x3]
no you don't have to.  The garbage collector will take care of that.
The also function should be used as the last line of your function.
I gotta run for a bit but will be back in about an hour or sooner.
Geomol
31-Mar-2009
[13347x3]
Yes, I understand that. What I don't understand, is how you call 
these functions, and what does on with the data, you get returned 
from your function. Do you have code examples online, I can look 
at?
what *goes* on ...
Using ALSO returns some data. If your code look like this:

mp3-data: load-mp3-function


and that load-mp3-function ends with an ALSO, setting the local var 
to none, you still have a full copy in mp3-data. Actually you end 
up having 2 copies for some time, until the garbage collector frees 
the local version. Later in your code, you need to set mp3-data to 
none to free to memory (which the garbage collector does). Now, is 
this how you use ALSO and why you need it?
Izkata
31-Mar-2009
[13350x5]
Most of mine are for conciseness - for example, when no data needs 
to be returned:

Client: first wait Listen
Data: also (copy Client) (close Client)
but, like before, say that happened in a function
Process: func [Client][
   ..do stuff..
   return also (copy Client) (close Client)
]

Client: first wait Listen
Logfile/add Process Client
I'll look for some actual code I'm using soon
Pread: func [URL /local Port][
   Port: open/binary URL
   Port/timeout: 3 * 60
   return also (copy Port) (close Port)
]
Geomol
31-Mar-2009
[13355x2]
What happens, if the connection fails?
Does my version work the same as yours beside the timeout?

newPread: func [URL] [read/binary URL]

And can the timeout be set somewhere in the system object?