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

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

6 replies
Charles F. Munat
Joined: 2008-12-29,
User offline. Last seen 42 years 45 weeks ago.

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

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...

List(red, yellow, blue).mkString(", ")

Does that work?

On Mar 5, 2009 6:33 PM, "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

Christian Szegedy
Joined: 2009-02-08,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...
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

Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...
You may want to define an abstract type to bind all these values into one thing.
Then you will get something cool for free...

     case class colorTriplet(blue: Boolean, green: Boolean, red: Boolean)
     val trip = colorTriplet(true, false, false)
     val textform = (for (n <- 0 until trip.productArity)
       yield trip.productElement(n)).mkString(", ")
     println(textform)   

(NOTE that the bolded out part is completely general stuf.. IMO it begs to be a part of std. lib)

Szymon

On Fri, Mar 6, 2009 at 3:33 AM, 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



--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...
Or rather, easily converting a Product to a Seq[Any], e.g.,

colorTriplet.toSeq.mkString(", ")

as part of the standard library would be nice.

alex


On Fri, Mar 6, 2009 at 4:52 AM, Szymon Jachim <sjachim@gmail.com> wrote:
You may want to define an abstract type to bind all these values into one thing.
Then you will get something cool for free...

     case class colorTriplet(blue: Boolean, green: Boolean, red: Boolean)
     val trip = colorTriplet(true, false, false)
     val textform = (for (n <- 0 until trip.productArity)
       yield trip.productElement(n)).mkString(", ")
     println(textform)   

(NOTE that the bolded out part is completely general stuf.. IMO it begs to be a part of std. lib)

Szymon

On Fri, Mar 6, 2009 at 3:33 AM, 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



--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ

Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Booleans to string immutably? An easy one, probably...
Hey that's exactly what my code is doing, but the name "toSeq" you came up with is really ok. ;-)

Sz.

On Fri, Mar 6, 2009 at 3:23 PM, Alex Boisvert <boisvert@intalio.com> wrote:
Or rather, easily converting a Product to a Seq[Any], e.g.,

colorTriplet.toSeq.mkString(", ")

as part of the standard library would be nice.

alex


On Fri, Mar 6, 2009 at 4:52 AM, Szymon Jachim <sjachim@gmail.com> wrote:
You may want to define an abstract type to bind all these values into one thing.
Then you will get something cool for free...

     case class colorTriplet(blue: Boolean, green: Boolean, red: Boolean)
     val trip = colorTriplet(true, false, false)
     val textform = (for (n <- 0 until trip.productArity)
       yield trip.productElement(n)).mkString(", ")
     println(textform)   

(NOTE that the bolded out part is completely general stuf.. IMO it begs to be a part of std. lib)

Szymon

On Fri, Mar 6, 2009 at 3:33 AM, 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



--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ




--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
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 looks interesting, but it prints:

true, false, false

I need it to print:

blue

Thanks.

Chas.

Szymon Jachim wrote:
> You may want to define an abstract type to bind all these values into
> one thing.
> Then you will get something cool for free...
>
> case class colorTriplet(blue: Boolean, green: Boolean, red: Boolean)
> val trip = colorTriplet(true, false, false)
> *val textform = (for (n <- 0 until trip.productArity)**
> ** yield trip.productElement(n))*.mkString(", ")
> println(textform)
>
> (NOTE that the bolded out part is completely general stuf.. IMO it begs
> to be a part of std. lib)
>
> Szymon
>
> On Fri, Mar 6, 2009 at 3:33 AM, 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
>
>
>
>

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