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

2020-05-25 06:44发布

This common pattern feels a bit verbose:

if (condition) 
  Some(result)
else None

I was thinking of using a function to simplify:

def on[A](cond: Boolean)(f: => A) = if (cond) Some(f) else None

This reduces the top example to:

on (condition) { result }

Does something like this exist already? Or is this overkill?

标签: scala
7条回答
走好不送
2楼-- · 2020-05-25 06:55

Similar to Scalaz, the Typelevel cats ecosystem has the mouse package with option:

scala> true.option("Its true!")
res0: Option[String] = Some(Its true!)
查看更多
时光不老,我们不散
3楼-- · 2020-05-25 06:57

You can use the PartialFunction companion object and condOpt:

PartialFunction.condOpt(condition) {case true => result}

Usage:

 scala> PartialFunction.condOpt(false) {case true => 42}
 res0: Option[Int] = None

 scala> PartialFunction.condOpt(true) {case true => 42}
 res1: Option[Int] = Some(42)
查看更多
Bombasti
4楼-- · 2020-05-25 06:58

Starting Scala 2.13, Option is now provided with the when builder which does just that:

Option.when(condition)(result)

For instance:

Option.when(true)(45)
// Option[Int] = Some(45)
Option.when(false)(45)
// Option[Int] = None

Also note the coupled unless method which does the opposite.

查看更多
Evening l夕情丶
5楼-- · 2020-05-25 06:59

Scalaz includes the option function:

import scalaz.syntax.std.boolean._

true.option("foo") // Some("foo")
false.option("bar") // None
查看更多
聊天终结者
6楼-- · 2020-05-25 07:00

Here is another approach that is quite straightforward:

Option(condition).collect{ case true => result }

A simple example:

scala> val enable = true
enable: Boolean = true

scala> Option(enable).collect{case true => "Yeah"}
res0: Option[String] = Some(Yeah)

scala> Option(!enable).collect{case true => "Yeah"}
res1: Option[String] = None

Here some advanced non-Boolean examples that put the condition into the pattern match:

val param = "beta"
Option(param).collect{case "alpha" => "first"} // gives None

Option(param).collect{case "alpha" => "first"
                      case "beta"  => "second"
                      case "gamma" => "third"} // gives Some(second)

val number = 999
Option(number).collect{case 0 => "zero"
                       case x if x > 10 => "too high"} // gives Some(too high)
查看更多
一夜七次
7楼-- · 2020-05-25 07:07
import scalaz._, Scalaz._
val r = (1 == 2) ? Some(f) | None
System.out.println("Res = " + r)
查看更多
登录 后发表回答