Hi Friedrich, I had a similar problem in Dolphin Smalltalk some days ago. Dolphin has String>>capitalized: capitalized "Answer a <readableString> which is a copy of the receiver but with the first character converted to its uppercase equivalent." | answer | (answer := self basicCopy) notEmpty ifTrue: [answer at: 1 put: self first asUppercase]. ^answer I was in a need of the opposite (I called it decapitalized) and ended with String>>withFirstCharacterTransformedWith: aMethodSymbol "Answer a <readableString> which is a copy of the receiver but with the first character converted by performing aMethodSymbol on it." | answer | (answer := self basicCopy) notEmpty ifTrue: [answer at: 1 put: (self first perform: aMethodSymbol)]. ^answer Comparing my solution with your first approach I like mine better :) But following your arguments about consistency I like your final approach best. Alas IMO it should be initiated by the ANSI Smalltalk team (is it still alive?) as it would be nice to have this in all dialects. Best regards Andreas Am 29.03.2013 um 07:55 schrieb Friedrich Dominicus <frido@q-software-solutions.de>:
The following two functions: capitalized "Return a copy with the first letter capitalized" | cap | self isEmpty ifTrue: [ ^self copy ]. cap := self copy. cap at: 1 put: (cap at: 1) asUppercase. ^ cap
and withFirstCharacterDownshifted "Return a copy with the first letter downShifted (in lower case)"
| answer | self ifEmpty: [ ^ self copy ]. answer := self copy. answer at: 1 put: answer first asLowercase. ^ answer
Well AFAIKT it should be first in the function (according to SLIME)
But as one can easily see it's redundant code. How about a replacement like: withFirstLetterTransformed: aTransformBlock | result | self isEmpty ifTrue: [^ self copy ]. result := self copy. result at: 1 put: (aTransformBlock value: result first ). ^ result
the above fucntions would get: capitalized "Return a copy with the first letter capitalized" ^ self withFirstLetterTransformed: [:value | value asUppercase ].
and withFirstCharacterDownshifted "Return a copy with the first letter downShifted (in lower case)"
^ self withFirstLetterTransformed: [:value | value asLowercase ].
Well I also know that capitalized is a very "old" thing but the second name is more specific. So what about: withFistCharacterUpshifted .... as counterpart?
There's another thing which probably is incongruent: we have first for accessing the first element of any collection but first: is not for setting the first element.
first:n yields the first n Elements of a collection.
That's not a good name then. firstElements:n would be a better name IMHO.
And then we could have first: someValue and so we would have in the end:
result first: (aTranformBlock value: result first).
would change the first Element of "any" collection. AFAIKT Lisp has such a convention...
my first changes to not to any harm to existing Code, but of course changing the meaning of first: would....
Regards Friedrich