Convert a List into an Option if it is populated

前端 未结 5 1896
北荒
北荒 2020-12-15 20:55

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[F         


        
5条回答
  •  悲哀的现实
    2020-12-15 21:17

    How about:

    Some(myList) collect { case(l@hd::tl) => Bar(l) }
    

    Seems pretty scala-esque to me.

提交回复
热议问题