join
Lex Spoon
lex at cc.gatech.edu
Wed Sep 27 07:27:30 UTC 2006
"J J" <azreal1977 at hotmail.com> writes:
> Actually, the implimentation that Keith had did a double dispatch as I
> recall (I guess I will dig that up and resend it if I ever get more
> then 5 minutes online at a time) and in this case, i.e. the argument
> to join: is a string, would result in a string.
That would sound fine to me, and it sounds so fine to you that
apparently you assumed this is what the original code did! However, I
just looked it up and it is not the case. Here's is the top of
Keith's message again:
> Whenever I find myself wanting to join a bunch of items together
> (e.g. to make a path) I am never satisfied with the result and so I
> took a look at the Collections/String classes to see whether anything
> fitted nicely.
>
> I came up with
>
> SequencableCollection>>join: aCollection
>
> ^ self class streamContents: [ :stream |
> aCollection
> do: [ :each | stream nextPut: each ]
> separatedBy: [ stream nextPutAll: self ] ]
It is great but for two things: it creates an instance of the
*receiver's* class, and it never calls asString.
When I read the idea, I thought of the following method:
join: separatorString
"Create a string composed of the elements of the receiver separated by the specified separator. For example: ['/', (#('usr' 'bin' 'squeak') join: '/')] or [#(1 2 3) join: ', ']"
^String streamContents: [ :result |
self
do: [ :each | result nextPutAll: each asString ]
separatedBy: [ result nextPutAll: separatorString ] ]
By the way, I still find "join" to be a funny name here, but everyone
in the thread insists it is common....
-Lex
More information about the Squeak-dev
mailing list
|