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 RegardsEdmondo

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

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 [at] gmx [dot] de> wrote:
easiest way : a.toList ++ list

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 [at] gmx [dot] 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>
> An: scala-user <scala-user [at] googlegroups [dot] 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

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 [at] gmx [dot] 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 [dot] porcu [at] gmail [dot] com>
> An: scala-user <scala-user [at] googlegroups [dot] com" target="_blank" rel="nofollow">scala-user [at] googlegroups [dot] 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


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 [dot] porcu [at] gmail [dot] 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

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