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

Why should I copy ?

 [1/4] from: gchillemi::aliceposta::it at: 24-Sep-2009 17:46


I have the following routine: rebol [] page: read/lines to-url "http://www.repubblica.it" foreach line page [ page-to-read: copy "http://" url-record: "www.plutus.com" page-to-read: append page-to-read reduce [url-record] print [page-to-read] ] the output is http://www.plutus.com Why if I remove copy from "page-to-read" rebol [] page: read/lines to-url "http://www.repubblica.it" foreach line page [ page-to-read: "http://" url-record: "www.plutus.com" page-to-read: append page-to-read reduce [url-record] print [page-to-read] ] The output is: http://www.plutus.com http://www.plutus.comwww.plutus.com http://www.plutus.comwww.plutus.comwww.plutus.com http://www.plutus.comwww.plutus.comwww.plutus.comwww.plutus.com http://www.plutus.comwww.plutus.comwww.plutus.comwww.plutus.comwww.plutus.co m http://www.plutus.comwww.plutus.comwww.plutus.comwww.plutus.comwww.plutus.co mwww.plutus.com http://www.plutus.comwww.plutus.comwww.plutus.comwww.plutus.comwww.plutus.co mwww.plutus.comw ww.plutus.com We are not inside an object and the string should be initialized by the command: page-to-read: "http://" Giuseppe Chillemi

 [2/4] from: dhsunanda::gmail at: 24-Sep-2009 17:22


It's probably easier to see what is going on with my example, where the equivalent code is a block in a function: ===== f: func [ /local blk ][ blk: [] for n 1 3 1 [ append blk n source f ] ] f f f ===== Watch the source of the 'f function as you run it....It keeps changing. That's because b: [] associates the word 'b with an empty block, []. Append value to 'b and [] is not empty any more! There is a lengthy discussion here: http://www.rebol.org/ml-display-thread.r?m=rmlRDLB *** Lesson: use COPY. it associates the word 'b with a fresh [] each time ===== f: func [ /local blk ][ blk: COPY [] for n 1 3 1 [ append blk n source f ] ] f f f ===== This behaviour can take a while to get used to ;) Sunanda

 [3/4] from: nick:guitarz at: 5-Oct-2009 9:52


Hi Giuseppe, In REBOL, strings are series. Any word label (variable) which refers to a series must be re-initialized using 'copy, if you want to clear its contents and assign new data. This is true of any series in REBOL, whether it's a block of data or a string of text. HTH :) - Nick Quoting Giuseppe Chillemi <gchillemi-aliceposta.it>:

 [4/4] from: moliad:gmai:l at: 5-Oct-2009 16:10


I had replied 2 weeks ago, but it seems my post never got to the mailing list, here it is... again. On Thu, Sep 24, 2009 at 12:22 PM, Sunanda <dhsunanda-gmail.com> wrote:
> Lesson: use COPY. it associates the word 'b with a fresh [] each time > > =====
......
> > > This behaviour can take a while to get used to ;) >
I just want to point out that this isn't' a bug, its a feature by design. =A0All rebol series are mutable, so they aren't copied each time they are assigned to a variable, like in most other languages, python for example. this means a big boost in assignment speed and it also means that series can be shared amongst many references by default, another thing which allows many optimizations. =A0Most other languages will create independent versions of series every time they are assigned, and sharing them becomes difficult. remember my function hack example, it doesn't copy the block created as storage for the function value... this is an example of how this can be used as the feature it is. this being said, there are some variants of function! creation code out there which creates a new independent context each time the function is called, but that is slow and may cause memory leaks in some use cases... better just to use copy all the time, this way its explicit and you realize you have full control. -MAx