0

I have a list of Integer which I need to convert to Option, however when List is empty i need to assign it as none. please suggest

val list: List[Int] = List()

val x = option(list)

if list is empty x should be none and if list has some value say List(1,2) then x should be Some(List(1,2))

please suggest

danD
  • 666
  • 1
  • 7
  • 29
  • What have you already tried by yourself? – cchantep Apr 16 '19 at 07:10
  • I have been trying x = Option(y) which returns some(List()). I have tried switch case which were giving compilation error. Could you please suggest – danD Apr 16 '19 at 07:13

1 Answers1

1
Option(list).filter(_.nonEmpty)
Tim
  • 26,753
  • 2
  • 16
  • 29