How to find a matching element in a list and map it in as an Scala API method?

后端 未结 4 500
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 06:42

Is there a method to do the following without doing both methods: find and map?

val l = 0 to 3
l.find(_ * 33 % 2 == 0).map(_ * 33) //          


        
4条回答
  •  自闭症患者
    2021-02-05 07:28

    This can do it, but it would be easier if you tell what you're really trying to achieve:

    l.flatMap(n => if (n * 33 % 2 == 0) Some(n * 33) else None).headOption
    

提交回复
热议问题