[ENH] String repeat

David N. Smith (IBM) dnsmith at watson.ibm.com
Tue May 30 19:00:01 UTC 2000


At 18:29 -0400 5/29/00, David N. Smith \(IBM\) wrote:
>At 8:54 +0200 5/29/00, Bert Freudenberg wrote:
>>On Sun, 28 May 2000, David N. Smith (IBM) wrote:
>>
>>> Attached is a method I've evolved over a while which does string
>>> repeats:
>>>
>>>	'0' rep: 5    ==>    '00000'
>>
>>0. Seems useful :-)
>>1. This should go up into SequencableCollection, not String.
>>2. It should have a more descriptive selector (at least repeat:).
> >3. Could be refactored to share the duplication code with from:to:put:.
>>
>>-- Bert
>
>Bert (and the others who replied):
>
>(1) Good idea; I just have never wanted it elsewhere so it stayed where I first put it.
>(2) Sure, repeat: is better.
>(3) I'll look at it...
>
>Dave

All:

(1) Upon a few moments contemplation I remembered why #rep: was in String; the algorithm was designed to be very fast and makes some assumptions:
	- The allocation of the collection is continuous (OrderedCollection is not)
	- A new collection can be obtained by #new:
	- The class of the object should not need to be known
Most collections fail in some way, either as with OrderedCollection which has empty space at the front and/or end, or as in collections which have more than one index (what does it mean to #repeat: a 2D array?).

(2) OK, it's now #repeat:.  Much better name.

(3) Hadn't seen that one; the algorithms are remarkably similar, though #from:to:put: only copies one element and #repeat: allows more than one (ie: #(1 2 3) repeat: 5). I agree that some refactoring is needed (and I added a note to that effect :-).

Attached is a new version. The old #rep: method is now in ArrayedCollection and is named #repeatPrivate:. There are implementations of #repeat: in Array, ByteArray, Character, String, and Symbol. ByteArray only works for itself but no subclasses.

There should be #repeat: methods for other collections, but they need a different algorithm which may well take some time to find. (Set is easy! Bag may not be.) For now, I leave it up to someone who has time to look at these. I was simply trying to share some code I had that I thought might be useful. If it isn't useful yet, I'll work on it later.

Dave
-------------- next part --------------
Skipped content of type multipart/appledouble-------------- next part --------------
_______________________________
David N. Smith
IBM T J Watson Research Center
Hawthorne, NY
_______________________________
Any opinions or recommendations
herein are those of the author  
and not of his employer.


More information about the Squeak-dev mailing list