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

Best way to prepone an option to a List

5 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear all,I have the following case:
val a :Option[Int] = Some(3);
val list = List(1,2,3);
I would like to create a new list prepending a to list.
Of course, if a is None no value has to be prepended.
What is the best way to do this?
Best RegardsEdmondo
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Best way to prepone an option to a List
There are many possible ways to do it, but I'd tend to
    Option(3).toList ::: list

  --Rex

On Wed, Jan 25, 2012 at 10:32 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all,I have the following case:
val a :Option[Int] = Some(3);
val list = List(1,2,3);
I would like to create a new list prepending a to list.
Of course, if a is None no value has to be prepended.
What is the best way to do this?
Best Regards Edmondo

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Best way to prepone an option to a List

easiest way : a.toList ++ list

-------- Original-Nachricht --------
> Datum: Wed, 25 Jan 2012 16:32:03 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Best way to prepone an option to a List

> Dear all,
> I have the following case:
>
> val a :Option[Int] = Some(3);
>
> val list = List(1,2,3);
>
> I would like to create a new list prepending a to list.
>
> Of course, if a is None no value has to be prepended.
>
> What is the best way to do this?
>
> Best Regards
> Edmondo

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: Best way to prepone an option to a List
Well... what is the question between ++ and ::: ?
Best Regards


2012/1/25 Dennis Haupt <h-star@gmx.de>
easiest way : a.toList ++ list

-------- Original-Nachricht --------
> Datum: Wed, 25 Jan 2012 16:32:03 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: scala-user <scala-user@googlegroups.com>
> Betreff: [scala-user] Best way to prepone an option to a List

> Dear all,
> I have the following case:
>
> val a :Option[Int] = Some(3);
>
> val list = List(1,2,3);
>
> I would like to create a new list prepending a to list.
>
> Of course, if a is None no value has to be prepended.
>
> What is the best way to do this?
>
> Best Regards
> Edmondo

Arjan Blokzijl
Joined: 2009-10-31,
User offline. Last seen 46 weeks 5 days ago.
Re: Best way to prepone an option to a List
a ++: list  might be even easier.

On 25 January 2012 16:44, Dennis Haupt <h-star@gmx.de> wrote:
easiest way : a.toList ++ list

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Best way to prepone an option to a List
++ delegates to ::: in case of a list
or something like this.
the :: and ::: methods are list only

Am 25.01.2012 16:53, schrieb Edmondo Porcu:
6QXs6jg [at] mail [dot] gmail [dot] com" type="cite">Well... what is the question between ++ and ::: ?
Best Regards


2012/1/25 Dennis Haupt <h-star [at] gmx [dot] de" target="_blank" rel="nofollow">h-star@gmx.de>
easiest way : a.toList ++ list

-------- Original-Nachricht --------
> Datum: Wed, 25 Jan 2012 16:32:03 +0100
> Von: Edmondo Porcu <edmondo [dot] porcu [at] gmail [dot] com" target="_blank" rel="nofollow">edmondo.porcu@gmail.com>
> An: scala-user <scala-user [at] googlegroups [dot] com" target="_blank" rel="nofollow">scala-user@googlegroups.com>
> Betreff: [scala-user] Best way to prepone an option to a List

> Dear all,
> I have the following case:
>
> val a :Option[Int] = Some(3);
>
> val list = List(1,2,3);
>
> I would like to create a new list prepending a to list.
>
> Of course, if a is None no value has to be prepended.
>
> What is the best way to do this?
>
> Best Regards
> Edmondo


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