Appending a character to a string

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Appending a character to a string

Damien Cassou-3
Hi,

Often, I want to concatenate a character to a string :

self doSomethingWith: myString, Character cr, anotherString.


However, #, only works with collections. I'm obliged to use #copyWith:
instead. Is there a pattern to do this without using Streams ?

Would it be desirable to enhance #, to allow concatenation of a single
element ?

Bye

--
Damien Cassou


Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Jon Hylands
On Sun, 11 Feb 2007 18:00:21 +0100, Damien Cassou
<[hidden email]> wrote:

> However, #, only works with collections. I'm obliged to use #copyWith:
> instead. Is there a pattern to do this without using Streams ?

self doSomethingWith: myString, (String with: Character cr), anotherString.

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Raptor (Small Biped Velociraptor Robot)
           http://www.huv.com/blog

Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Philippe Marschall
2007/2/11, Jon Hylands <[hidden email]>:
> On Sun, 11 Feb 2007 18:00:21 +0100, Damien Cassou
> <[hidden email]> wrote:
>
> > However, #, only works with collections. I'm obliged to use #copyWith:
> > instead. Is there a pattern to do this without using Streams ?
>
> self doSomethingWith: myString, (String with: Character cr), anotherString.

I find that quite ugly. For many things like this there are:
String space
String cr
String crlf
String tab


Philippe

> Later,
> Jon
>
> --------------------------------------------------------------
>    Jon Hylands      [hidden email]      http://www.huv.com/jon
>
>   Project: Micro Raptor (Small Biped Velociraptor Robot)
>            http://www.huv.com/blog
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Nicolas Cellier-3
Philippe Marschall a écrit :

> 2007/2/11, Jon Hylands <[hidden email]>:
>> On Sun, 11 Feb 2007 18:00:21 +0100, Damien Cassou
>> <[hidden email]> wrote:
>>
>> > However, #, only works with collections. I'm obliged to use #copyWith:
>> > instead. Is there a pattern to do this without using Streams ?
>>
>> self doSomethingWith: myString, (String with: Character cr),
>> anotherString.
>
> I find that quite ugly. For many things like this there are:
> String space
> String cr
> String crlf
> String tab
>
>
> Philippe
>
>> Later,
>> Jon
>>
>> --------------------------------------------------------------
>>    Jon Hylands      [hidden email]      http://www.huv.com/jon
>>
>>   Project: Micro Raptor (Small Biped Velociraptor Robot)
>>            http://www.huv.com/blog
>>
>>
>
>

Hello Damien,
Concatenating characters with #, would not be shocking me.
Maybe i'am short minded, but i do not see what else would be the meaning
of Character>>#,
Of course, you would have String>>#, implementation complexified, and
semantic of #, not exactly the super one, but then what?

Doing so, String cr,tab and co would become quite needless.

This could eventually be generalized to any immediate collection
(ByteArray, WordArray, FloatArray and co).

However, generalization to any object and any collection (pointer form)
would be ambiguous, since we can have collection of collection...

I already saw #, used for creating 3D Points (VW Jun), or to concatenate
numbers into vectors (Jeffrey Hallman's Smallpack), and i used it myself
once for similar purposes (constructing matrices with , for column
concatenation and ,, for row concatenations).

Problem of such extensions is that they are likely to collide if you
want to load several packages in one image... Not recommanded

However, I do not see yet a good readon to not try that for Character.

Nicolas


Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Adrian Lienhard
In reply to this post by Philippe Marschall
...or use

String streamContents: [...]

Adrian

On Feb 11, 2007, at 18:22 , Philippe Marschall wrote:

> 2007/2/11, Jon Hylands <[hidden email]>:
>> On Sun, 11 Feb 2007 18:00:21 +0100, Damien Cassou
>> <[hidden email]> wrote:
>>
>> > However, #, only works with collections. I'm obliged to use  
>> #copyWith:
>> > instead. Is there a pattern to do this without using Streams ?
>>
>> self doSomethingWith: myString, (String with: Character cr),  
>> anotherString.
>
> I find that quite ugly. For many things like this there are:
> String space
> String cr
> String crlf
> String tab
>
>
> Philippe
>
>> Later,
>> Jon
>>
>> --------------------------------------------------------------
>>    Jon Hylands      [hidden email]      http://www.huv.com/jon
>>
>>   Project: Micro Raptor (Small Biped Velociraptor Robot)
>>            http://www.huv.com/blog
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Alan Kay
Hi Folks --

Damien is asking very reasonable questions, and I don't think that
work-around answers are in the spirit of his questions. The Smalltalk
ancestors of Smalltalk-80 had better answers to both of these, which
were inspired by similar practices in both Lisp and APL.

Cheers,

Alan


Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Joshua Gargus-2
In reply to this post by Damien Cassou-3
Tweak allows this (at least the Croquet version of Tweak).  In the  
Tweak-Hacks package, you find the method:

, anObject
        ^ self copyReplaceFrom: self size + 1
                  to: self size
                  with: anObject asString

The nice thing is that it doesn't just work for characters.  You can  
append *anything* to a string.

I've found this to be very handy, and haven't run into any subtle  
bugs/problems as a result.  However, my primary use of the construct  
is for logging, which isn't a very demanding application.

Josh



On Feb 11, 2007, at 9:00 AM, Damien Cassou wrote:

> Hi,
>
> Often, I want to concatenate a character to a string :
>
> self doSomethingWith: myString, Character cr, anotherString.
>
>
> However, #, only works with collections. I'm obliged to use #copyWith:
> instead. Is there a pattern to do this without using Streams ?
>
> Would it be desirable to enhance #, to allow concatenation of a single
> element ?
>
> Bye
>
> --
> Damien Cassou
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

dpharris
In reply to this post by Alan Kay
Quoting Alan Kay <[hidden email]>:

> Hi Folks --
>
> Damien is asking very reasonable questions, and I don't think that
> work-around answers are in the spirit of his questions. The Smalltalk
> ancestors of Smalltalk-80 had better answers to both of these, which
> were inspired by similar practices in both Lisp and APL.
>
> Cheers,
>
> Alan
>
>
>
Alan-

It would have been nice for you to actually tell us what those answers are,
rather than titillating us ;-).  

While I have followed Smalltalk for years, I have only recently started a
project where I need to use it.  I have found it rather frustrating in that,  
while I can discover the answers by rooting through the classes, it is not
easy.  I, too, was surprised that I could not do things with collections, but
rather had to use a subclass.  The adding a character problem cropped up very
early in my explorations, and all the solutions seemed to be excessively
wordy.  

I expect part of my problem is unfamiliarity with the (vast) set of tools
available to discover the needed methods.  However, this is part of the large
learning curve of Smalltalk that people complain about.  

Actually, if someone could remind me of how to find a method by my giving an
example --- 1 ? 1 -> 2 .  I know this exists, but have no idea where to find
it, not documentation on it.  

Thanks,
David






Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Giovanni Corriga
In reply to this post by Damien Cassou-3
Il giorno dom, 11/02/2007 alle 18.00 +0100, Damien Cassou ha scritto:

> Hi,
>
> Often, I want to concatenate a character to a string :
>
> self doSomethingWith: myString, Character cr, anotherString.
>
>
> However, #, only works with collections. I'm obliged to use #copyWith:
> instead. Is there a pattern to do this without using Streams ?
>
> Would it be desirable to enhance #, to allow concatenation of a single
> element ?

Personally, yes. It also shouldn't be too hard to implement a
double-dispatch mechanism for that.

        Giovanni


Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Frank Shearar
In reply to this post by dpharris
<[hidden email]> asks

> Actually, if someone could remind me of how to find a method by my giving
an
> example --- 1 ? 1 -> 2 .  I know this exists, but have no idea where to
find
> it, not documentation on it.

That would be the Method Finder/Selector Browser. It lives in the Tools flap
(at least, in Squeak 3.8).

frank


Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Damien Cassou-3
In reply to this post by Philippe Marschall
Philippe Marschall a écrit :

> 2007/2/11, Jon Hylands <[hidden email]>:
>> On Sun, 11 Feb 2007 18:00:21 +0100, Damien Cassou
>> <[hidden email]> wrote:
>>
>> > However, #, only works with collections. I'm obliged to use #copyWith:
>> > instead. Is there a pattern to do this without using Streams ?
>>
>> self doSomethingWith: myString, (String with: Character cr),
>> anotherString.
>
> I find that quite ugly. For many things like this there are:
> String space
> String cr
> String crlf
> String tab

Nice try Philippe, however, there is no String>>space :-) (at least in
my squeak-dev-76 image).



--
Damien Cassou

Reply | Threaded
Open this post in threaded view
|

Re: Appending a character to a string

Klaus D. Witzel
In reply to this post by dpharris
On Sun, 11 Feb 2007 19:26:56 +0100, <[hidden email]> wrote:

> Quoting Alan Kay <[hidden email]>:
>
>> Hi Folks --
>>
>> Damien is asking very reasonable questions, and I don't think that
>> work-around answers are in the spirit of his questions. The Smalltalk
>> ancestors of Smalltalk-80 had better answers to both of these, which
>> were inspired by similar practices in both Lisp and APL.
>>
>> Cheers,
>>
>> Alan
>>
>>
>>
> Alan-
>
> It would have been nice for you to actually tell us what those answers  
> are,
> rather than titillating us ;-).
>
> While I have followed Smalltalk for years, I have only recently started a
> project where I need to use it.  I have found it rather frustrating in  
> that,
> while I can discover the answers by rooting through the classes, it is  
> not
> easy.  I, too, was surprised that I could not do things with  
> collections, but
> rather had to use a subclass.  The adding a character problem cropped up  
> very
> early in my explorations, and all the solutions seemed to be excessively
> wordy.

SequenceableCollection>>#copyWith: newElement
        "Answer a copy of the receiver that is 1 bigger
         than the receiver and has newElement as the
         last element."

This is from the (famous) Blue Book and the comment is still the same in  
the Squeak image. Nothing has less words *and* is faster (in Smalltalk,  
that is ;-)

> I expect part of my problem is unfamiliarity with the (vast) set of tools
> available to discover the needed methods.  However, this is part of the  
> large
> learning curve of Smalltalk that people complain about.
>
> Actually, if someone could remind me of how to find a method by my  
> giving an
> example --- 1 ? 1 -> 2 .  I know this exists, but have no idea where to  
> find
> it, not documentation on it.

As Frank already wrote, you might want to use, from the Tools flap,  
"Method Finder".

For the example in this thread, this tool suggests, for

  'ab'. $c. 'abc'

to use

'ab' copyWith: $c => 'abc'

HTH.

/Klaus

> Thanks,
> David
>
>
>
>
>
>
>



Reply | Threaded
Open this post in threaded view
|

RE: Appending a character to a string

Alan L. Lovejoy
In reply to this post by Damien Cassou-3
<Damien Cassou>
Often, I want to concatenate a character to a string :

self doSomethingWith: myString, Character cr, anotherString.


However, #, only works with collections. I'm obliged to use #copyWith:
instead. Is there a pattern to do this without using Streams ?
</Damien Cassou>

A conceptual example:

SequenceableCollection>>& anElementOrSequenceableCollection
        "#(1 2 3) & 4"" -> #(1 2 3 4)"
        "#(1 2 3) & #(4 5 6)"" -> #(1 2 3 4 5 6)"

        ^anElementOrSequenceableCollection appendToSequenceableCollection:
self

SequenceableCollection>>appendToSequenceableCollection:
aSequenceableCollection
        ^aSequenceableCollection, self

Object>>appendToSequenceableCollection: aSequenceableCollection
        ^aSequenceableCollection copyWith: self