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

Re: Booleans to string immutably? An easy one, probably...

6 replies
Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Yes but, given his example mkString on List(blue, red) will return "true, true" not "blue, red" as Charles indicates he wants.

I'd say you'd be better off implementing your colour values as instances of a proper enumerated type (called Colour) and then work from that (assuming of course that you have a finite known enumeration of colours you want to work from).

Ishaaq

2009/3/6 Christian Szegedy <christian.szegedy@gmail.com>
I just want to say one word to you - just one word : mkString

On 3/5/09, Charles F. Munat <chas@munat.com> wrote:
I have a set of boolean vals. For the sake of this question, let's say they are colors:

val blue = true
val green = false
val red = true

I want to go through this set of vals and create a string with the colors in a comma-separated list, thus:

val colors: String = "blue, red"

I can think of lots of ways to do this mutably, but does anyone have a clever way to do it without resort to vars or ArrayBuffers?

Thanks.

Chas. Munat


Tony Morris
Joined: 2008-12-19,
User offline. Last seen 30 weeks 4 days ago.
Re: Booleans to string immutably? An easy one, probably...

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

scala> val blue=true; val green=false; val red=true; val z =
List(("blue", blue), ("green", green), ("red", red)) filter (_._2) map
(_._1) mkString (", ")
blue: Boolean = true
green: Boolean = false
red: Boolean = true
z: String = blue, red

Ishaaq Chandy wrote:
> Yes but, given his example mkString on List(blue, red) will return
> "true, true" not "blue, red" as Charles indicates he wants.
>
> I'd say you'd be better off implementing your colour values as
> instances of a proper enumerated type (called Colour) and then work
> from that (assuming of course that you have a finite known
> enumeration of colours you want to work from).
>
> Ishaaq
>
> 2009/3/6 Christian Szegedy >
>
> I just want to say one word to you - just one word : mkString
>
>
> On 3/5/09, *Charles F. Munat* > wrote:
>
> I have a set of boolean vals. For the sake of this question, let's
> say they are colors:
>
> val blue = true val green = false val red = true
>
> I want to go through this set of vals and create a string with the
> colors in a comma-separated list, thus:
>
> val colors: String = "blue, red"
>
> I can think of lots of ways to do this mutably, but does anyone
> have a clever way to do it without resort to vars or ArrayBuffers?
>
> Thanks.
>
> Chas. Munat
>
>
>

- --
Tony Morris
http://tmorris.net/

S, K and I ought to be enough for anybody.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmwkXsACgkQmnpgrYe6r62i3QCgsNjxe5nIF2xP/v7PddmBORlY
0uMAn3rF1Xther7dQGyIkD++3p9LwVDm
=/k0x
-----END PGP SIGNATURE-----

Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...
Here's another variation...

object Flags {
 case class Flag(name: String, value: Boolean)
 def apply(l: Flag*) = l filter (_.value) map (_.name) mkString ", "
}

import Flags._

val blue = Flag("blue", true)
val green = Flag("green", false)
val red = Flag("red", true)

scala> Flags(blue, green, red)
res0: String = blue, red

Charles F. Munat
Joined: 2008-12-29,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...

Ishaaq Chandy wrote:
> Yes but, given his example mkString on List(blue, red) will return
> "true, true" not "blue, red" as Charles indicates he wants.

Yes, that's the problem with the previous replies. (Also, the mkString
part is easy. I got that -- though I should have mentioned it to avoid
confusion. The trick is to go from val names to strings.)

> I'd say you'd be better off implementing your colour values as instances
> of a proper enumerated type (called Colour) and then work from that
> (assuming of course that you have a finite known enumeration of colours
> you want to work from).

I'll have to think about this. It's a nice idea, but I'm not clear at
the moment how it solves the initial problem.

Thanks! (And thanks, David and Christian. I appreciate your replies.)

Chas.
>
> Ishaaq
>
> 2009/3/6 Christian Szegedy >
>
> I just want to say one word to you - just one word : mkString
>
>
> On 3/5/09, *Charles F. Munat* > wrote:
>
> I have a set of boolean vals. For the sake of this question,
> let's say they are colors:
>
> val blue = true
> val green = false
> val red = true
>
> I want to go through this set of vals and create a string with
> the colors in a comma-separated list, thus:
>
> val colors: String = "blue, red"
>
> I can think of lots of ways to do this mutably, but does anyone
> have a clever way to do it without resort to vars or ArrayBuffers?
>
> Thanks.
>
> Chas. Munat
>
>
>

Charles F. Munat
Joined: 2008-12-29,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...

This is very clever. I have to persist the colors, however, so I think
the other method suggested by Tony using vals will be easier. I like
this one, though.

Thanks,

Chas.

Alex Boisvert wrote:
> Here's another variation...
>
> object Flags {
> case class Flag(name: String, value: Boolean)
> def apply(l: Flag*) = l filter (_.value) map (_.name) mkString ", "
> }
>
> import Flags._
>
> val blue = Flag("blue", true)
> val green = Flag("green", false)
> val red = Flag("red", true)
>
> scala> Flags(blue, green, red)
> res0: String = blue, red
>

Charles F. Munat
Joined: 2008-12-29,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...

Perfect. Thanks.

Chas.

Tony Morris wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> scala> val blue=true; val green=false; val red=true; val z =
> List(("blue", blue), ("green", green), ("red", red)) filter (_._2) map
> (_._1) mkString (", ")
> blue: Boolean = true
> green: Boolean = false
> red: Boolean = true
> z: String = blue, red
>
> Ishaaq Chandy wrote:
>> Yes but, given his example mkString on List(blue, red) will return
>> "true, true" not "blue, red" as Charles indicates he wants.
>>
>> I'd say you'd be better off implementing your colour values as
>> instances of a proper enumerated type (called Colour) and then work
>> from that (assuming of course that you have a finite known
>> enumeration of colours you want to work from).
>>
>> Ishaaq
>>
>> 2009/3/6 Christian Szegedy > >
>>
>> I just want to say one word to you - just one word : mkString
>>
>>
>> On 3/5/09, *Charles F. Munat* > > wrote:
>>
>> I have a set of boolean vals. For the sake of this question, let's
>> say they are colors:
>>
>> val blue = true val green = false val red = true
>>
>> I want to go through this set of vals and create a string with the
>> colors in a comma-separated list, thus:
>>
>> val colors: String = "blue, red"
>>
>> I can think of lots of ways to do this mutably, but does anyone
>> have a clever way to do it without resort to vars or ArrayBuffers?
>>
>> Thanks.
>>
>> Chas. Munat
>>
>>
>>
>
> - --
> Tony Morris
> http://tmorris.net/
>
> S, K and I ought to be enough for anybody.
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkmwkXsACgkQmnpgrYe6r62i3QCgsNjxe5nIF2xP/v7PddmBORlY
> 0uMAn3rF1Xther7dQGyIkD++3p9LwVDm
> =/k0x
> -----END PGP SIGNATURE-----
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Booleans to string immutably? An easy one, probably...

On Fri, Mar 06, 2009 at 10:44:37AM -0800, Charles F. Munat wrote:
> Yes, that's the problem with the previous replies. (Also, the mkString
> part is easy. I got that -- though I should have mentioned it to avoid
> confusion. The trick is to go from val names to strings.)

In the not really serious suggestion category, there's also:

scala> val flags = List('blue -> true, 'red -> false, 'green -> true)
flags: List[(Symbol, Boolean)] = List(('blue,true), ('red,false), ('green,true))

scala> flags filter (_._2) map (_._1) mkString " "
res0: String = 'blue 'green

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