Definition: | sequence s = repeat(object x, integer i) |
Description: | Create a sequence of length i where each element is x. |
Comments: |
When you repeat a sequence or a floating-point number the interpreter does not actually make multiple copies in memory.
Rather, a single copy is "pointed to" a number of times.
For efficiency reasons, if the item to be repeated appears to be a character, ie is an integer in the range 7 to 255, a string is created, otherwise a dword-sequence is created. Better overall performance was also observed by making repeat(0,N) create a dword-sequence rather than a string of nulls which was very likely to be auto-expanded to a dword sequence in the near future. Generally speaking, all further handling of the result is completely transparent whether a string or dword-sequence is created, with the following exceptions: performance gains, explicit tests with the string() builtin, #isginfo directives, accessing it using inline assembly, and lastly holding raw binary can be done in a string as one byte per element, rather than 4 or 8, which might need a secondary poke() before use. Compatibility note: RDS Eu/OpenEuphoria allow an atom length/count, whereas Phix requires an integer, since I believe that deliberately trapping say 11.99 (which RDS Eu/OpenEuphoria truncate to 11) may help to expose the occasional programming error at source, rather than ten minutes later when the sequence turns out to be one element too short. |
Example 1: |
repeat(0, 10) -- {0,0,0,0,0,0,0,0,0,0} |
Example 2: |
repeat("JOHN", 4) -- {"JOHN", "JOHN", "JOHN", "JOHN"} -- The interpreter creates only one copy of "JOHN" in memory, -- and increases the reference count of that one item by 4. |
Example 3: |
repeat('=', 10) -- "==========" (a string) |
See Also: | append, prepend, sequence-formation operator |
