- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Booleans to string immutably? An easy one, probably...
Fri, 2009-03-06, 03:33
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
Fri, 2009-03-06, 03:57
#2
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:
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
Fri, 2009-03-06, 14:07
#3
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:
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
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ıɥʇ
Fri, 2009-03-06, 15:37
#4
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:
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ıɥʇ
Fri, 2009-03-06, 15:57
#5
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:
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
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ıɥʇ
Fri, 2009-03-06, 19:47
#6
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
>
>
>
>
List(red, yellow, blue).mkString(", ")
Does that work?