[squeak-dev] Smalltalk string API

Eliot Miranda eliot.miranda at gmail.com
Tue Feb 22 18:03:04 UTC 2011


On Tue, Feb 22, 2011 at 9:59 AM, Levente Uzonyi <leves at elte.hu> wrote:

> 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'.
>

got it. finally.  sorry :)


>
>
> 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
>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20110222/8f799405/attachment.htm


More information about the Squeak-dev mailing list