Question: I have a method that should convert a list to an `Option` of an object, or `None` if the list is empty.

```
def listToOption(myList: List[Foo]): Option[Bar] = {
  if(myList.nonEmpty) Some(Bar(myList))
  else None
}

case class Bar(fooList: List[Foo]) {}

```

For some reason, my solution feels rather inelegant, and not the Scala way. It seems I should be able to use a method on `List` to do this sort of thing, but I can't wrap my head around it.

Is there a more Scala-like way to do this?

Answer: 
Starting `Scala 2.13`, `Option` has a [`when`](https://www.scala-lang.org/api/2.13.x/scala/Option$.html#when[A](cond:Boolean)(a:=%3EA):Option[A]) builder:

```none
Option.when(condition)(result)

```

which in our case gives:

```
Option.when(myList.nonEmpty)(Bar(myList))
// val myList = List[Int]()    =>    Option[Bar] = None
// val myList = List(1, 2)     =>    Option[Bar] = Some(Bar(List(1, 2)))

```

Also note [`Option.unless`](https://www.scala-lang.org/api/2.13.x/scala/Option$.html#unless[A](cond:Boolean)(a:=%3EA):Option[A]) which promotes the opposite condition:

```
Option.unless(myList.isEmpty)(Bar(myList))
// val myList = List[Int]()    =>    Option[Bar] = None
// val myList = List(1, 2)     =>    Option[Bar] = Some(Bar(List(1, 2)))

```
```
import scalaz._; import Scalaz._
myList.toNel.map(Bar)

```

`toNel` - is "to non-empty list" here, it returns `Option[NonEmptyList]` for safety:

```
scala> case class Bar(a: NonEmptyList[Int])
defined class Bar

scala> List(1,2,3).toNel.map(Bar)
res64: Option[Bar] = Some(Bar(NonEmptyList(1, 2, 3)))

scala> List[Int]().toNel.map(Bar)
res65: Option[Bar] = None

```