- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Seeking Elegance
Tue, 2012-01-24, 20:30
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
Tue, 2012-01-24, 21:11
#2
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
Tue, 2012-01-24, 21:21
#3
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
>
Tue, 2012-01-24, 21:41
#4
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:
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
Tue, 2012-01-24, 22:21
#5
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.
Tue, 2012-01-24, 22:41
#6
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
Tue, 2012-01-24, 22:51
#7
Re: Re: Seeking Elegance
On Tue, Jan 24, 2012 at 10:20 PM, Raoul Duke <raould@gmail.com> wrote:
More relevant, Scalex:
http://scalex.org/?q=String+%3D%3E+String
-jason
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
Thu, 2012-01-26, 20:41
#8
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.
>
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.