问题
if reelID = reelWeights.Count - 1
then Array.fold calc1 (0L,0) reelWeights.[reelID]
else Array.fold calc2 (0L,0) reelWeights.[reelID]
I tried use pipeline, it seems to slow down a little bit (not sure why):
reelWeights.[reelID]
|> (if reelID = reelWeights.Count - 1 then Array.fold calc1 else Array.fold calc2) (0L,0)
if I do
let calc x = if x then calc1 else calc2
Array.fold (calc reelID = reelWeights.Count - 1) (0L,0) reelWeights.[reelID]
then it looks nice at the cost of redundantly check conditionion in loops.
回答1:
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]
回答2:
Or in one line:
let weight =
Array.fold (if reelID = (reelWeights.Count - 1) then calc1 else calc2) (0L,0) reelWeights.[reelID]
来源:https://stackoverflow.com/questions/12521841/is-there-any-way-to-simplify-this-line