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 |
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 <[hidden email]>: > 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 > |
On Mar 29, 2013, at 10:28 AM, Andreas Wacknitz <[hidden email]> wrote: > > > 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?) ANSI is dead since a long time. Marcus |
In reply to this post by FDominicus
>
> > the above fucntions would get: > capitalized > "Return a copy with the first letter capitalized" > ^ self withFirstLetterTransformed: [:value | value asUppercase ]. > > and > withFirstCharacterDownshifted withFirstCharacterLowercase? > "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? does not fly :) > 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. indeed. > 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…. Did you check what are the senders of first:? STef |
Am 30.03.2013 um 20:19 schrieb stephane ducasse <[hidden email]>: Did you check what are the senders of first:? I don't. But I can imagine there are loads of senders of next: :) Same case, no? Norbert
|
Administrator
|
In reply to this post by FDominicus
I see what you mean... Apple's dictionary says that capitalized could mean "all caps" or "first letter capitalized", so I guess it's ambiguous. For #withFirstCharacterDownshifted, isn't downshifted misleading? It only works on letters. For instance, it doesn't change $: to $;. I would change #withFirstCharacterDownshifted->#withFirstCharacterLowercase. And if I changed #capitalized, it would be to something like #withFirstCharacterCapitalized (or uppercase). Also, the method you extracted uses "letter" instead of "character". They should all be uniform.
Cheers,
Sean |
Sean P. DeNigris wrote:
> FDominicus wrote > >> Well I also know that capitalized is a very "old" thing but the second >> name is more specific. So what about: >> withFistCharacterUpshifted .... as counterpart? >> > > I see what you mean... Apple's dictionary says that capitalized could mean > "all caps" or "first letter capitalized", so I guess it's ambiguous. > > For #withFirstCharacterDownshifted, isn't downshifted misleading? It only > works on letters. For instance, it doesn't change $: to $;. I would change > #withFirstCharacterDownshifted->#withFirstCharacterLowercase. > > And if I changed #capitalized, it would be to something like > #withFirstCharacterCapitalized (or uppercase). Also, the method you > extracted uses "letter" instead of "character". They should all be uniform. > > > > ----- > Cheers, > Sean > -- > View this message in context: http://forum.world.st/I-stumbled-upon-tp4678839p4679024.html > Sent from the Pharo Smalltalk mailing list archive at Nabble.com. > > > That could just be #withFirstCapitalized - you already know you are dealing with characters (?) ... |
In reply to this post by stephane ducasse
stephane ducasse <[hidden email]> writes:
>> >> >> the above fucntions would get: >> capitalized >> "Return a copy with the first letter capitalized" >> ^ self withFirstLetterTransformed: [:value | value asUppercase ]. >> >> and >> withFirstCharacterDownshifted > > withFirstCharacterLowercase? withFirstCharacterInLowercase? withFirstCharacterInUppercase? > > >> "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? > > does not fly :) unifiying it a bit... > > Did you check what are the senders of first:? No, but I checked today. There are 35 senders of it in my Image (pharo 2-0 one click) Regards Friedrich |
In reply to this post by Sean P. DeNigris
"Sean P. DeNigris" <[hidden email]> writes:
> FDominicus wrote >> Well I also know that capitalized is a very "old" thing but the second >> name is more specific. So what about: >> withFistCharacterUpshifted .... as counterpart? > > I see what you mean... Apple's dictionary says that capitalized could mean > "all caps" or "first letter capitalized", so I guess it's ambiguous. > > For #withFirstCharacterDownshifted, isn't downshifted misleading? It only > works on letters. For instance, it doesn't change $: to $;. I would change > #withFirstCharacterDownshifted->#withFirstCharacterLowercase. > > And if I changed #capitalized, it would be to something like > #withFirstCharacterCapitalized (or uppercase). Also, the method you > extracted uses "letter" instead of "character". They should all be > uniform. proposed to change that. The name are not from me but what there is in the current image. So there is capitalize and there is withFirstCharacterDownshifted. And the have a very similiar implementation which I just put into another message. But I've not idea on how to make a proposal, but in this way. Sending it to this mailing list... Regards Friedrich |
Free forum by Nabble | Edit this page |