1
if reelID = reelWeights.Count - 1
    then Array.fold calc1 (0L,0) reelWeights.[reelID]
    else Array.fold calc2 (0L,0) reelWeights.[reelID]

我尝试使用管道,它似乎有点慢(不知道为什么):

reelWeights.[reelID]
    |> (if reelID = reelWeights.Count - 1 then Array.fold calc1 else Array.fold calc2) (0L,0)

如果我做

let calc x = if x then calc1 else calc2
Array.fold (calc reelID = reelWeights.Count - 1) (0L,0) reelWeights.[reelID]

那么它看起来不错,代价是在循环中冗余检查条件。

4

2 回答 2

5

Assuming calc1 and calc2 have the same signature (or if they're values rather than functions, are the same type):

let calc = if reelID = reelWeights.Count - 1 then calc1 else calc2
Array.fold calc (0L, 0) reelWeights.[reelID]
于 2012-09-20T22:49:14.857 回答
1

或者在一行中:

let weight = 
    Array.fold (if reelID = (reelWeights.Count - 1) then calc1 else calc2) (0L,0) reelWeights.[reelID]
于 2012-09-21T08:01:20.100 回答