[squeak-dev] Smalltalk string API

Levente Uzonyi leves at elte.hu
Tue Feb 22 17:59:07 UTC 2011


On Tue, 22 Feb 2011, Eliot Miranda wrote:

> On Mon, Feb 21, 2011 at 9:37 PM, Levente Uzonyi <leves at elte.hu> wrote:
>
>> On Mon, 21 Feb 2011, Eliot Miranda wrote:
>>
>> snip
>>
>>
>>  Actually, thinking a little, why not
>>>
>>>
>>> Collection >> join: delimiter
>>>
>>>      ^self species streamContents: [ :stream |
>>>              self
>>>                      do: [ :each | each putOn: stream ]
>>>                      separatedBy: [ delimiter putOn: stream ] ]
>>>
>>> ?
>>>
>>
>> Because I'd like this:
>> #('foo' 'bar' 'baz') join: $-
>> to return this:
>> 'foo-bar-baz'
>> rather than this:
>> #($f $o $o $- $b $a $r $- $b $a $z).
>>
>
> So then why not
>
> SequenceableCollection >> join: delimiter
>
>       ^self isEmpty
>           ifTrue: [ self ]
>           ifFalse:
>                [ self first species streamContents:
>                     [ :stream |
>                     self
>                         do: [ :each | each putOn: stream ]
>                         separatedBy: [ delimiter putOn: stream ] ] ]
>
> with first replaced by anyOne in Collection?

This assumes that the receiver is a collection of objects whose species is 
Array or a subclass of String (#streamContents: doesn't work with other 
collections IIRC). If that's the case, then there's no need to use 
#putOn:, because #nextPutAll: does the same and it's faster too.

With my version, this:
#(1 2 3) join: $- 
returns this:
'1-2-3'.


Levente

>
>
>>
>> Pharo's implementation dispatches on the class of the delimiter. That's
>> fine, but requires multiple methods. Also I've never used #join: to create
>> anything else but Strings.
>>
>>
>> Levente
>>
>> snip
>>
>>
>



More information about the Squeak-dev mailing list