有没有更好的方法在Scala中执行以下if/else处理?

有没有更好的方法在Scala中执行以下if/else处理?,scala,Scala,我有一个事件驱动系统,它对交易做出如下响应 def onTrade { if (price > threshold) { processReject() return } if (volume > threshold) { processReject() return } . . . } 我想我可以通过定义一个内部方法来改进语法 def onTrade {

我有一个事件驱动系统,它对交易做出如下响应

def onTrade {

    if (price > threshold) {
        processReject()
        return
    } 

    if (volume > threshold) {
        processReject()
        return
    } 
    .
    .
    .
}
我想我可以通过定义一个内部方法来改进语法

def onTrade {

    def filterRemove = (filter: Boolean) => {
        if (filter) {
            processReject()
        }
        filter
    }

    val filters = List(
        filterRemove(price > threshold),
        filterRemove(volume > threshold),...
    )

    if (filters.filter(x => x == true).size > 0) return

}

语法更清晰,尤其是随着过滤器数量的增加。我遇到的一个问题是,代码通过每次测试而不是在第一次失败时返回,从而浪费了不必要的时钟周期。这有什么办法吗?例如,在filterRemove返回false时立即退出onTrade。如果有更具表现力的方法,我也很想知道。

例如,您可以维护拒绝条件列表。然后可以使用exists按顺序调用它们。一旦条件为false,迭代就结束

下面是一个小例子:

val conditions: List[() => Boolean] =
  List(
    () => price > threshold1,
    () => volume > threshold2
  )

def checkAndReject() = {
  val filter = conditions.exists( _() )
  if( filter ) processReject()
  filter
}

如果你只是想把条件写在适当的地方,有什么不简单的用or呢?它是半严格的

if (
  price > threshold1
  || volume > threshold2
  || ...
) {
   processReject()
   return
}
... proceed

这不是一种非常实用或惯用的风格,但可能很有趣:

def onTrade {
    val reject = () => {
        processReject()
        return
    }

    // ...

    if (price > threshold) reject()

    // ...

    if (volume > threshold) reject()

    // ...

}

问题是你有一个布尔值列表——它们都已经被处理过了。你需要推迟。例如:

def onTrade {

    // now filterRemove returns Function0[Boolean]
    // also, filter is passed by-name, so it isn't 
    // processed until you call the Function0
    def filterRemove(filter: => Boolean) = () => {
        if (filter) {
            processReject()
            true
        } else false
    }

    // This won't process anything, just accumulate Function0's
    val filters = List(
        filterRemove(price > threshold),
        filterRemove(volume > threshold),...
    )

    // And we use "exists", which returns on the first true value
    if (filters.exists(x => x)) return

}

如果没有太多的阈值需要检查,我将执行以下操作:

def process(price:Int, volume:Int) = (price, volume) match {
   case (x:Int, y:Int) if x > threshold1 || y > threshold2 => println ("reject")
   case _ =>
}
def threshold1 = 10
def threshold2:Int = throw new Exception // redefine to throw exception
def process(price:Int, volume:Int) = (price, volume) match {
   case (x:Int, y:Int) if x > threshold1 || y > threshold2 => println ("reject")
   case _ =>
}

process(12, 22)
process(5, 22) 
测试如下:

def process(price:Int, volume:Int) = (price, volume) match {
   case (x:Int, y:Int) if x > threshold1 || y > threshold2 => println ("reject")
   case _ =>
}
def threshold1 = 10
def threshold2:Int = throw new Exception // redefine to throw exception
def process(price:Int, volume:Int) = (price, volume) match {
   case (x:Int, y:Int) if x > threshold1 || y > threshold2 => println ("reject")
   case _ =>
}

process(12, 22)
process(5, 22) 

看看这篇文章:它涵盖了相同的领域和非常相似的问题。这也是我的想法。除非我在后面有一个其他子句,而不是返回,因为在一个方法中间返回通常不是一个好主意。