6

I could do this with an if an statement, but there is probably a "scala" way to do this.

  def notScalaConv(in: Seq[String]): Option[Seq[String]] = {
    if (in.isEmpty)
      None
    else
      Option(in)
  }
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
marathon
  • 7,881
  • 17
  • 74
  • 137
  • `if` is an expression in Scala, which is uncommon in other language: using it this way looks "scalaish" to me – Bruno Grieder Feb 24 '17 at 18:43
  • 1
    As you wrote it, your `if` wouldn't work, because the value of a one-branch `if` is always `Unit`. I've corrected it by adding `else`. – Seth Tisue Feb 24 '17 at 19:13

1 Answers1

18

You can lift your Seq to an Option, and then filter it. Like this:

 def notScalaConv(in: Seq[String]): Option[Seq[String]] = {
     Option(in).filter(_.nonEmpty)
 }
marstran
  • 26,413
  • 5
  • 61
  • 67