2011-10-04 3 views
0

私は、私は構文は、特にフィルタの数としてクリーナーです内側の方法Scalaで/ elseを処理する場合、以下のような方法がありますか?

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 

} 

を定義することによって、構文的に物事を改善することができたと思っ

def onTrade { 

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

    if (volume > threshold) { 
     processReject() 
     return 
    } 
    . 
    . 
    . 
} 

を次のように取引に反応するイベント駆動システムを持っています増加する。私が持っている1つの問題は、最初の失敗に戻るのではなく、すべての単一のテストを通過することによってコードが不要なクロックサイクルを無駄にすることです。これは何かこれの周りの方法ですか?たとえば、filterRemoveがfalseを返すとすぐにonTradeを終了します。これを行うためのより表現力のある方法があるなら、私はそれも知っているのが大好きです。

+0

は、この記事を見てください: http://debasishg.blogspot.com/2010/12/composable-domain-models-using-scalaz.html同じドメインと非常によく似た問題を扱っています。 – CheatEx

答えて

4

問題は、あなたがブール値のリストを持っていることである:それは、これは非常に機能的または慣用のスタイルではありませんが、興味のある厳格な

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

たとえば、拒否する条件のリストを維持することができます。その後、existsで順番に呼び出すことができます。条件がfalseになるとすぐに、繰り返しが終了します。あなたは単にかを使用していないものを、場所で条件を記述しようとしている場合

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

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

:ここ

は小さな例ですか? - それらはすべて持っている

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

    // ... 

    if (price > threshold) reject() 

    // ... 

    if (volume > threshold) reject() 

    // ... 

} 
+0

これも私が考えたものです。私は、 'return'の代わりに' else'節が続くことを除いて、メソッドの途中で戻り値を持つのは一般的には良い考えではないので、 –

3

適切半ですすでに処理されています。それを遅らせる必要があります。

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 

} 
+0

私は「拒否」を「怠惰な弁」に変えることさえできると思います... –

0

チェックするためにあまりにも多くのしきい値が存在しない場合は、次のように、私はどうなる:たとえば

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) 
関連する問題