Simplify if (x) Some(y) else None?

后端 未结 7 1896
旧时难觅i
旧时难觅i 2021-02-05 02:46

This common pattern feels a bit verbose:

if (condition) 
  Some(result)
else None

I was thinking of using a function to simplify:



        
7条回答
  •  忘了有多久
    2021-02-05 03:22

    import scalaz._, Scalaz._
    val r = (1 == 2) ? Some(f) | None
    System.out.println("Res = " + r)
    

提交回复
热议问题