有什么办法可以简化这条线吗? [英] is there any way to simplify this line?

查看:59
本文介绍了有什么办法可以简化这条线吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)

如果我愿意

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.

推荐答案

假定calc1calc2具有相同的签名(或者,如果它们是值而不是函数,则具有相同的类型):

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]

这篇关于有什么办法可以简化这条线吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆