Convert a List into an Option if it is populated

前端 未结 5 1890
北荒
北荒 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:06

    Lee's answer is good, but I think this corresponds to the intention a bit more clearly:

    Option(myList).filter(_.nonEmpty).map(Bar)
    

提交回复
热议问题