[vwnc] capitalized strings

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

[vwnc] capitalized strings

Stefan Schmiedl
While composing the other message, I found myself once again searching
for a method capitalizing a string, i.e. ensuring that the first letter
is upper case. This is what I found:

AbstractWorkspace and Tools.FileTools.ParcelFileViewer both implement a
private method "capitalized: aString" with cosmetic differences.

A rewriter search for

        `string at: 1 put: `string first asUppercase

returned several places:

        Refactory.Browser.CompositeLintRule class>>ruleFor:protocol:
        Refactory.Browser.Prerequisite>>printOn:
        Tools.FileTools:ParcelFileViewer>>capitalized:
        CodeWriter>>storeSourceHeader
        InteractiveCompilerErrorHandler>>declareNewClass:from:
        ExternalInterfaceBuilder>>updateExternalLabel
        ExternalInterfaceBuilder>>updateCategorySelected
        ManualWriter>>reportExtendedDefinitionFor:on:
        Net.HeaderField>>canonicalFieldName
        ProfileOutlineBrowser>>printReport:on:

The package Squeak-Extensions defines capitalized for Symbol and String,
using (`string at: 1) instead of `string first, so I checked for that,
too:

        ICC.ICC1Utils class>>validateClassName:confirm:warn:
        ICC.IncrementalTypes.IT1FullType>>capitalizedFor:
        String>>capitalized
        SystemUtils class>>validateClassName:confirm:warn:

These are "just the facts" from my current development image.
Pros and Cons re adding String>>capitalized to the base system?

Thanks,
s.
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
jas
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

jas
At 02:18 PM 6/13/2008, Stefan Schmiedl wrote:

of discovering a plethora of mutants,
scattered about, whereas he (and we)
might well prefer something else.

...details snipped...

>These are "just the facts" from my current development image.
>Pros and Cons re adding String>>capitalized to the base system?


The name #capitalized strikes me as ambiguous -
does it update in place, or answer a value?


String>>capitalize
        " mutable Strings can self modify, (hence the verb)
          but these's another problem...
        "
        self at: 1 put: (self at: 1) asUppercase

String>>asCapitalized
        " One could over-optimize this,
          and answer self - iff already capitalized,
          but that lacks important properties.
          Could instead answer a copy for that case,
          which lacks fewer of said properties,
          but is still lacking.
          This is better: (still has a problem, though)
        "
        ^self copyReplaceFrom: 1 to: 1 with: (self at: 1) asUppercase


So TSTTCPW appears to me to be

String>>asCapitalized
        ^self first asUppercase, (self copyFrom: 2)


Note: I'm assuming that #, is implemented on Character,
and that aString respondsTo: #copyFrom:,
neither of which is true, but should be.


Regards,

-cstb



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

Andre Schnoor
In reply to this post by Stefan Schmiedl

Am 13.06.2008 um 23:18 schrieb Stefan Schmiedl:

> While composing the other message, I found myself once again searching
> for a method capitalizing a string, i.e. ensuring that the first  
> letter
> is upper case.

String >> asMixedcase
        | copy |
        self size == 0 ifTrue:[^self].
        copy := self species new: self size.
        copy at: 1 put: (self at: 1) asUppercase.
        self size > 1 ifTrue:[
                2 to: self size do:
                        [:index | copy at: index put: (self at: index) asLowercase]].
        ^copy


Andre

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

Stefan Schmiedl
Hi Andre,

thanks for your code, it made me think!

On Sat, 14 Jun 2008 14:53:27 +0200
Andre Schnoor <[hidden email]> wrote:

>
> Am 13.06.2008 um 23:18 schrieb Stefan Schmiedl:
>
> > While composing the other message, I found myself once again searching
> > for a method capitalizing a string, i.e. ensuring that the first  
> > letter is upper case.
>
> String >> asMixedcase
> | copy |
> self size == 0 ifTrue:[^self].
> copy := self species new: self size.
> copy at: 1 put: (self at: 1) asUppercase.
> self size > 1 ifTrue:[
> 2 to: self size do:
> [:index | copy at: index put: (self at: index) asLowercase]].
> ^copy

hmm... you're forcing the rest of the string to lower case. I have not
yet needed to do this, but if I would have to, I'd lowercase the whole
string and only uppercase (are those verbs?) the first character.

Do you have a specific reason why you're implementing everything
by yourself? Using stuff that is already there, the code would shorten
to:

CharacterArray>>asMixedcaseStefan1
        | copy |
        self isEmpty ifTrue: [^self].
        copy := self asLowercase.
        copy at: 1 put: copy first asUppercase.
        ^copy

You could even get rid of the local variable:

CharacterArray>>asMixedcaseStefan2
        self isEmpty ifTrue: [^ self].
        ^ (self asLowercase)
                at: 1 put: self first asUppercase;
                yourself

Ok, now I've got me curious:

| t1 |
[ t1 := 'frischGemauertInDerErdenStehtDieFormAusLehmGebrannt' asMixedcaseAndre ] benchmarkMicrosecondsToRunRepetitions: 1000000
 2.04113 1.65703 2.06479 1.85676 1.84561 1.69418 1.69391

| t2 |
[ t2 := 'frischGemauertInDerErdenStehtDieFormAusLehmGebrannt' asMixedcaseStefan1 ] benchmarkMicrosecondsToRunRepetitions: 1000000
 1.96859 1.9153 1.95549 1.96726 1.70966 1.91922 2.12955

| t3 |
[ t3 := 'frischGemauertInDerErdenStehtDieFormAusLehmGebrannt' asMixedcaseStefan2 ] benchmarkMicrosecondsToRunRepetitions: 1000000
 1.7299 1.73017 1.6999 1.72513 1.73166 1.73661 1.73176 1.72689

So it's not the concern for runtime that made you write this.
Historical reasons?

Just making Smalltalk,
s.
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

Alan Darlington
In reply to this post by Stefan Schmiedl
All of the methods described below assume that the string has at least one character, and some assume that it has at least two characters.  Do you really want to throw exceptions on empty and single-character strings?

    Cheers,
    Alan

----- Original Message ----
From: cstb <[hidden email]>
To: Stefan Schmiedl <[hidden email]>
Cc: "[hidden email]" <[hidden email]>
Sent: Friday, June 13, 2008 10:11:04 PM
Subject: Re: [vwnc] capitalized strings

At 02:18 PM 6/13/2008, Stefan Schmiedl wrote:

of discovering a plethora of mutants,
scattered about, whereas he (and we)
might well prefer something else.

...details snipped...

>These are "just the facts" from my current development image.
>Pros and Cons re adding String>>capitalized to the base system?


The name #capitalized strikes me as ambiguous -
does it update in place, or answer a value?


String>>capitalize
        " mutable Strings can self modify, (hence the verb)
          but these's another problem...
        "
        self at: 1 put: (self at: 1) asUppercase

String>>asCapitalized
        " One could over-optimize this,
          and answer self - iff already capitalized,
          but that lacks important properties.
          Could instead answer a copy for that case,
          which lacks fewer of said properties,
          but is still lacking.
          This is better: (still has a problem, though)
        "
        ^self copyReplaceFrom: 1 to: 1 with: (self at: 1) asUppercase


So TSTTCPW appears to me to be

String>>asCapitalized
        ^self first asUppercase, (self copyFrom: 2)


Note: I'm assuming that #, is implemented on Character,
and that aString respondsTo: #copyFrom:,
neither of which is true, but should be.


Regards,

-cstb



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

Travis Griggs-3
In reply to this post by Stefan Schmiedl

On Jun 14, 2008, at 12:41 PM, Stefan Schmiedl wrote:

> Hi Andre,
>
> thanks for your code, it made me think!
>
> On Sat, 14 Jun 2008 14:53:27 +0200
> Andre Schnoor <[hidden email]> wrote:
>
>>
>> Am 13.06.2008 um 23:18 schrieb Stefan Schmiedl:
>>
>>> While composing the other message, I found myself once again  
>>> searching
>>> for a method capitalizing a string, i.e. ensuring that the first
>>> letter is upper case.
>>
>> String >> asMixedcase
>> | copy |
>> self size == 0 ifTrue:[^self].
>> copy := self species new: self size.
>> copy at: 1 put: (self at: 1) asUppercase.
>> self size > 1 ifTrue:[
>> 2 to: self size do:
>> [:index | copy at: index put: (self at: index) asLowercase]].
>> ^copy
>
> hmm... you're forcing the rest of the string to lower case. I have not
> yet needed to do this, but if I would have to, I'd lowercase the whole
> string and only uppercase (are those verbs?) the first character.
>
> Do you have a specific reason why you're implementing everything
> by yourself? Using stuff that is already there, the code would shorten
> to:
>
> CharacterArray>>asMixedcaseStefan1
> | copy |
> self isEmpty ifTrue: [^self].
> copy := self asLowercase.
> copy at: 1 put: copy first asUppercase.
> ^copy
>
> You could even get rid of the local variable:
>
> CharacterArray>>asMixedcaseStefan2
> self isEmpty ifTrue: [^ self].
> ^ (self asLowercase)
> at: 1 put: self first asUppercase;
> yourself

I'd forgo worrying about speed until someone showed me it mattered and  
do:

CharacterArray>>raiseFirst
(self first: 1) asUppercase , (self allButFirst: 1)

It expressed pretty clear (imho) and doesn't have to worry about empty  
collections.

If I was having a moment of Dolphin envy/nostalgia, I might be tempted  
to implement

CharacterArray>>raiseFirst
       
        | block |
        block := [:toRaise |
                block := [:toPass | toPass].
                toRaise asUppercase].
        ^self collect: [:each | block value: each]

I'm being silly here on the second, but it's a fun thinker.

--
Travis Griggs
Objologist
"I think that we should be men first, and subjects afterward." - Henry  
David Thoreau



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
jas
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

jas
In reply to this post by Alan Darlington
At 12:49 PM 6/14/2008, Alan Darlington wrote:
All of the methods described below assume that the string has at least one character,


Correct - the precondition for all of them is [self notEmpty].


 and some assume that it has at least two characters.


No - you're guessing a different implementation
for #copyFrom: than I intended - which is fair,
since I didn't specify it.

I implement it as an alias for #allButFirst:.


 Do you really want to throw exceptions on empty and single-character strings?


Yes - I do.

Since #capitalized is defined in terms of #first,
empty strings refuse capitalization, thus matching
the exception-behavior of that which they depend on,
figuratively speaking.

Seemed only fair to make it literally true, as well.


Cheers,

-cstb



 


    Cheers,
    Alan

----- Original Message ----
From: cstb <[hidden email]>
To: Stefan Schmiedl <[hidden email]>
Cc: "[hidden email]" <[hidden email]>
Sent: Friday, June 13, 2008 10:11:04 PM
Subject: Re: [vwnc] capitalized strings

At 02:18 PM 6/13/2008, Stefan Schmiedl wrote:

of discovering a plethora of mutants,
scattered about, whereas he (and we)
might well prefer something else.

...details snipped...

>These are "just the facts" from my current development image.
>Pros and Cons re adding String>>capitalized to the base system?


The name #capitalized strikes me as ambiguous -
does it update in place, or answer a value?


String>>capitalize
        " mutable Strings can self modify, (hence the verb)
          but these's another problem...
        "
        self at: 1 put: (self at: 1) asUppercase

String>>asCapitalized
        " One could over-optimize this,
          and answer self - iff already capitalized,
          but that lacks important properties.
          Could instead answer a copy for that case,
          which lacks fewer of said properties,
          but is still lacking.
          This is better: (still has a problem, though)
        "
        ^self copyReplaceFrom: 1 to: 1 with: (self at: 1) asUppercase


So TSTTCPW appears to me to be

String>>asCapitalized
        ^self first asUppercase, (self copyFrom: 2)


Note: I'm assuming that #, is implemented on Character,
and that aString respondsTo: #copyFrom:,
neither of which is true, but should be.


Regards,

-cstb



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
jas
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

jas
In reply to this post by Travis Griggs-3
At 04:05 PM 6/14/2008, Travis Griggs wrote:

>On Jun 14, 2008, at 12:41 PM, Stefan Schmiedl wrote:
>
>> Hi Andre,
>>
>> thanks for your code, it made me think!


+1.


>>
>> On Sat, 14 Jun 2008 14:53:27 +0200
>> Andre Schnoor <[hidden email]> wrote:
>>
>>>
>>> Am 13.06.2008 um 23:18 schrieb Stefan Schmiedl:
>>>
>>>> While composing the other message, I found myself once again  
>>>> searching
>>>> for a method capitalizing a string, i.e. ensuring that the first
>>>> letter is upper case.
>>>
>>> String >> asMixedcase
>>>      | copy |
>>>      self size == 0 ifTrue:[^self].
>>>      copy := self species new: self size.
>>>      copy at: 1 put: (self at: 1) asUppercase.
>>>      self size > 1 ifTrue:[
>>>              2 to: self size do:
>>>                      [:index | copy at: index put: (self at: index) asLowercase]].
>>>      ^copy
>>
>> hmm... you're forcing the rest of the string to lower case. I have not
>> yet needed to do this, but if I would have to, I'd lowercase the whole
>> string and only uppercase (are those verbs?) the first character.
>>
>> Do you have a specific reason why you're implementing everything
>> by yourself? Using stuff that is already there, the code would shorten
>> to:
>>
>> CharacterArray>>asMixedcaseStefan1
>>       | copy |
>>       self isEmpty ifTrue: [^self].
>>       copy := self asLowercase.
>>       copy at: 1 put: copy first asUppercase.
>>       ^copy
>>
>> You could even get rid of the local variable:
>>
>> CharacterArray>>asMixedcaseStefan2
>>       self isEmpty ifTrue: [^ self].
>>       ^ (self asLowercase)
>>               at: 1 put: self first asUppercase;
>>               yourself
>
>I'd forgo worrying about speed until someone showed me it mattered and  
>do:
>
>CharacterArray>>raiseFirst
>(self first: 1) asUppercase , (self allButFirst: 1)


Hey - that first: bit is a nice touch,
even though it produces such horribly
pragmatic results, rubidiomatically,
some might say.  Can't allow it.


Otherwise, you're advocating almost the same solution,
for about the same reason, excepting the exception.

Except that, having seen yours, mine becomes

>>capitalized
        ^self first asUppercase, self allButFirst


Yes, I know - I started out claiming #capitalized
was an ambiguous name.  Unfortunately, that was
Fd Up BS, and also wrong.  ;-)


>>asProperNoun
        ^self first asUppercase, self allButFirst asLowercase

>>capitalizedOr: a0block
        ^self isEmpty
                ifTrue: [a0block value]
                ifFalse: [self capitalized]

>>asMixedCase
        ^(self allSatisfy: [:ch| ch isUppercase])
                ifTrue: [self first asLowercase, self allButFirst]
                ifFalse: [self capitalized]


Cheers,

-cstb

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

Andre Schnoor
In reply to this post by Travis Griggs-3

> On Jun 14, 2008, at 12:41 PM, Stefan Schmiedl wrote:
>
>> Do you have a specific reason why you're implementing everything
>> by yourself? Using stuff that is already there, the code would  
>> shorten
>> to: ....

I didn't mean the code to be especially elegant or short. Just copied  
it from my current image for instant use. It's simple, handles the  
case of empty strings and doesn't create much garbage. IIRC, I once  
copied and modified it from somewhere else in the String hierarchy.  
Did not much think about it.

Short and elegant expressions look nice (and Smalltalk is tempting in  
this regard), but they tend to incur ineffeciency (garbage,  
unnecessary level of abstraction). Below a certain threshold of  
bytecode size, it can sometimes be more effective to do things directly.

Andre
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

Stefan Schmiedl
On Sun, 15 Jun 2008 14:06:40 +0200
Andre Schnoor <[hidden email]> wrote:

>
> > On Jun 14, 2008, at 12:41 PM, Stefan Schmiedl wrote:
> >
> >> Do you have a specific reason why you're implementing everything
> >> by yourself? Using stuff that is already there, the code would  
> >> shorten
> >> to: ....
>
> I didn't mean the code to be especially elegant or short. Just copied  
> it from my current image for instant use. It's simple, handles the  
> case of empty strings and doesn't create much garbage. IIRC, I once  
> copied and modified it from somewhere else in the String hierarchy.  
> Did not much think about it.

Thanks for the explanation. I'm often wondering about the why
of things for fear of missing something. Copy and tweak is a quite
success technique for problem solving.

> Short and elegant expressions look nice (and Smalltalk is tempting in  
> this regard), but they tend to incur ineffeciency (garbage,  
> unnecessary level of abstraction). Below a certain threshold of  
> bytecode size, it can sometimes be more effective to do things directly.

That's what I'm often wondering, so imagine my surprise when
there was no runtime difference between your "direct manipulation"
and reusing the available methods.

Just making Smalltalk,
s.
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] capitalized strings

giorgiof
In reply to this post by Travis Griggs-3
Hi,
I have on my VW image the method that was in VSE. I like the name (but I'm using it from at least 15 years...)
Travis, the name raiseFirst is one I'll never browse for if I need to find if something that make a change in the sense of capitalizing or converting to uppercase is already there. Probably this is because English is not my first language, but seems to me not the right name.
In any case, I'll probably search the system (browse implementors...) for *Uppercase* or *Capitalize*, surely never for *Raise*
For the History .... Here is the version and comment from original VSE implementation: (the one living on my VW image, lazily copied..)

asFirstUppercase
        "(public) Answer a String containing the
         receiver with first character in upper-case."

    | answer |
    (self size < 2) ifTrue: [^self asUppercase].
    answer := self copyFrom: 1 to: (self size).
    answer at: 1 put: ( self at: 1 ) asUppercase.
    ^answer

ciao

Giorgio

On Sun, Jun 15, 2008 at 1:05 AM, Travis Griggs <[hidden email]> wrote:

On Jun 14, 2008, at 12:41 PM, Stefan Schmiedl wrote:

> Hi Andre,
>
> thanks for your code, it made me think!
>
> On Sat, 14 Jun 2008 14:53:27 +0200
> Andre Schnoor <[hidden email]> wrote:
>
>>
>> Am 13.06.2008 um 23:18 schrieb Stefan Schmiedl:
>>
>>> While composing the other message, I found myself once again
>>> searching
>>> for a method capitalizing a string, i.e. ensuring that the first
>>> letter is upper case.
>>
>> String >> asMixedcase
>>      | copy |
>>      self size == 0 ifTrue:[^self].
>>      copy := self species new: self size.
>>      copy at: 1 put: (self at: 1) asUppercase.
>>      self size > 1 ifTrue:[
>>              2 to: self size do:
>>                      [:index | copy at: index put: (self at: index) asLowercase]].
>>      ^copy
>
> hmm... you're forcing the rest of the string to lower case. I have not
> yet needed to do this, but if I would have to, I'd lowercase the whole
> string and only uppercase (are those verbs?) the first character.
>
> Do you have a specific reason why you're implementing everything
> by yourself? Using stuff that is already there, the code would shorten
> to:
>
> CharacterArray>>asMixedcaseStefan1
>       | copy |
>       self isEmpty ifTrue: [^self].
>       copy := self asLowercase.
>       copy at: 1 put: copy first asUppercase.
>       ^copy
>
> You could even get rid of the local variable:
>
> CharacterArray>>asMixedcaseStefan2
>       self isEmpty ifTrue: [^ self].
>       ^ (self asLowercase)
>               at: 1 put: self first asUppercase;
>               yourself

I'd forgo worrying about speed until someone showed me it mattered and
do:

CharacterArray>>raiseFirst
(self first: 1) asUppercase , (self allButFirst: 1)

It expressed pretty clear (imho) and doesn't have to worry about empty
collections.

If I was having a moment of Dolphin envy/nostalgia, I might be tempted
to implement

CharacterArray>>raiseFirst

       | block |
       block := [:toRaise |
               block := [:toPass | toPass].
               toRaise asUppercase].
       ^self collect: [:each | block value: each]

I'm being silly here on the second, but it's a fun thinker.

--
Travis Griggs
Objologist
"I think that we should be men first, and subjects afterward." - Henry
David Thoreau



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc