is there any way to simplify this line?

五迷三道 提交于 2019-12-11 11:34:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!