[Newbies] pretty printing
bert at freudenbergs.de
Tue Sep 30 19:33:28 UTC 2008
Am 29.09.2008 um 09:52 schrieb Claus Kick:
> Bert Freudenberg wrote:
>> Am 28.09.2008 um 09:52 schrieb Randal L. Schwartz:
>>> aString collect: [:char | char xmlEscaped].
> I had actually in mind to do it this way - but I thought the do:
> is easier to read. Runtime performance is always step 2 for me.
>>> Internally, that uses a Stream, which extends itself nicely as
>>> new data
It does not use a Stream, #collect: knows the size of the collection
in advance, it's the same as the original one.
You were probably thinking of something like
^String streamContents: [:strm | aString do: [:c | sstrm nextPutAll:
In some images there is #gather: which does this more concisely but
unfortunately uses Array instead of "self species" so it would not
work with Strings.
>>> In your version, the early string data is getting repeatedly
>>> to make each new string. Ouch.
> That may be, my Squeak is rusty (VW these days).
I'm pretty certain it would be the same in VW because #, has to copy
(unless you apply serious amounts of magic).
>> Err, #collect: constructs a string character by character, not
>> from other strings. Double-ouch ;)
> So, the gist is - whether you do do: or collect: doesnt matter
Well, #collect: is much more readable and concise, and might be
optimized for some sorts of collections.
In general it's good practice to use the most specific iteration
method available rather than emulating using #do: (and always think
twice, no, three times before using #to:do:).
- Bert -
More information about the Beginners