This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Seeking Elegance

8 replies
kolotyluk
Joined: 2010-06-04,
User offline. Last seen 5 weeks 15 hours ago.

I have

val commandArray = command.toLowerCase().toCharArray()
commandArray(0) = commandArray(0).toUpperCase
val className = commandArray.toString()

as a way to convert a string to lower case and capitalize the first
character, but I suspect there may be a more elegant way to do this in
Scala. Can anyone think of a more elegant way that is clear?

Cheers, Eric

d_m
Joined: 2010-11-11,
User offline. Last seen 35 weeks 2 days ago.
Re: Seeking Elegance

On Tue, Jan 24, 2012 at 11:30:53AM -0800, Eric Kolotyluk wrote:
> I have
>
> val commandArray = command.toLowerCase().toCharArray()
> commandArray(0) = commandArray(0).toUpperCase
> val className = commandArray.toString()
>
> as a way to convert a string to lower case and capitalize the first
> character, but I suspect there may be a more elegant way to do this
> in Scala. Can anyone think of a more elegant way that is clear?

I would have thought of something more like:

def titleCase(s:String) = s(0).toUpper + s.substring(1).toLower

Elegance is in the eye of the beholder, but this method involves less
work and is shorter.

Doug Tangren
Joined: 2009-12-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Seeking Elegance



On Tue, Jan 24, 2012 at 2:42 PM, Erik Osheim <erik@plastic-idolatry.com> wrote:
On Tue, Jan 24, 2012 at 11:30:53AM -0800, Eric Kolotyluk wrote:
> I have
>
>         val commandArray = command.toLowerCase().toCharArray()
>         commandArray(0) = commandArray(0).toUpperCase
>         val className = commandArray.toString()
>
> as a way to convert a string to lower case and capitalize the first
> character, but I suspect there may be a more elegant way to do this
> in Scala. Can anyone think of a more elegant way that is clear?

I would have thought of something more like:

 def titleCase(s:String) = s(0).toUpper + s.substring(1).toLower

Elegance is in the eye of the beholder, but this method involves less
work and is shorter.


"bIpPy".toLowerCase.capitalize
 
Andreas Hofstadler
Joined: 2009-02-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Seeking Elegance

Hi,

command.toLowerCase.capitalize

capitalize is defined in scala.collection.immutable.StringOps

Greets, Andi

On 2012-01-24 20:30, Eric Kolotyluk wrote:
> I have
>
> val commandArray = command.toLowerCase().toCharArray()
> commandArray(0) = commandArray(0).toUpperCase
> val className = commandArray.toString()
>
> as a way to convert a string to lower case and capitalize the first
> character, but I suspect there may be a more elegant way to do this in
> Scala. Can anyone think of a more elegant way that is clear?
>
> Cheers, Eric
>

kolotyluk
Joined: 2010-06-04,
User offline. Last seen 5 weeks 15 hours ago.
Re: Seeking Elegance
OK - very elegant - thanks :-)

The nice thing about Scala is there are all kinds of nice little convenience things like 'capitalize' around, but they are not always easy to find.

Using Java in Eclipse I would have been more likely to stumble across something like this myself because if I typed toLowerCase. in the editor, then a tool-tip with a listing of the available methods would have appeared including the javadoc comments. The Scala editor only gives you the method name so I would not have known what the capitalize method actually does.

Cheers, Eric

On 2012-01-24 11:46 AM, Doug Tangren wrote:
CAJ2WPXgdSdpCc5fA-PT0GwOiPwDBVCddoS_MpCNcHbBVgo4sBA [at] mail [dot] gmail [dot] com" type="cite">


On Tue, Jan 24, 2012 at 2:42 PM, Erik Osheim <erik [at] plastic-idolatry [dot] com" rel="nofollow">erik@plastic-idolatry.com> wrote:
On Tue, Jan 24, 2012 at 11:30:53AM -0800, Eric Kolotyluk wrote:
> I have
>
>         val commandArray = command.toLowerCase().toCharArray()
>         commandArray(0) = commandArray(0).toUpperCase
>         val className = commandArray.toString()
>
> as a way to convert a string to lower case and capitalize the first
> character, but I suspect there may be a more elegant way to do this
> in Scala. Can anyone think of a more elegant way that is clear?

I would have thought of something more like:

 def titleCase(s:String) = s(0).toUpper + s.substring(1).toLower

Elegance is in the eye of the beholder, but this method involves less
work and is shorter.


"bIpPy".toLowerCase.capitalize
 
-- Erik

Sophie
Joined: 2011-11-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Seeking Elegance

> The nice thing about Scala is there are all kinds of nice little
> convenience things like 'capitalize' around, but they are not always
> easy to find.

Squeak Smalltalk has this nifty "find by example" facility: feed in
example input and outputs, and it finds methods that match the need.

http://wiki.squeak.org/squeak/1916

Raoul Duke
Joined: 2009-01-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Seeking Elegance

On Tue, Jan 24, 2012 at 1:11 PM, Sophie wrote:
> Squeak Smalltalk has this nifty "find by example" facility: feed in example
> input and outputs, and it finds methods that match the need.
> http://wiki.squeak.org/squeak/1916

see
http://www.haskell.org/hoogle/
too

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: Seeking Elegance
On Tue, Jan 24, 2012 at 10:20 PM, Raoul Duke <raould@gmail.com> wrote:
On Tue, Jan 24, 2012 at 1:11 PM, Sophie <itsme213@hotmail.com> wrote:
> Squeak Smalltalk has this nifty "find by example" facility: feed in example
> input and outputs, and it finds methods that match the need.
> http://wiki.squeak.org/squeak/1916

see
http://www.haskell.org/hoogle/
too

More relevant, Scalex:
http://scalex.org/?q=String+%3D%3E+String
-jason
kolotyluk
Joined: 2010-06-04,
User offline. Last seen 5 weeks 15 hours ago.
Re: Seeking Elegance

I am impressed at how many cool ways there are to do this. While

command.toLowerCase.capitalize

is handy, the current Scala support in Eclipse does not make it easy to
understand the function of capitalize. This method and

def titleCase(s: String) = s.head.toUpperCase + s.tail.toLowerCase

c/o Kevin Wright, make it exceedingly clear what is going on without any
documentation.

Cheers, Eric

On 2012-01-24 11:42 AM, Erik Osheim wrote:
> On Tue, Jan 24, 2012 at 11:30:53AM -0800, Eric Kolotyluk wrote:
>> I have
>>
>> val commandArray = command.toLowerCase().toCharArray()
>> commandArray(0) = commandArray(0).toUpperCase
>> val className = commandArray.toString()
>>
>> as a way to convert a string to lower case and capitalize the first
>> character, but I suspect there may be a more elegant way to do this
>> in Scala. Can anyone think of a more elegant way that is clear?
> I would have thought of something more like:
>
> def titleCase(s:String) = s(0).toUpper + s.substring(1).toLower
>
> Elegance is in the eye of the beholder, but this method involves less
> work and is shorter.
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland