在F#中,我需要做这样的事情:
let price k s t r v =
let d1 = d1 k s t r v
... and so on
Run Code Online (Sandbox Code Playgroud)
当他们被传递到函数中时,我已经厌倦了列出所有参数.除了将参数转换为参数对象(我不能做的事情)之外,还有什么方法可以对参数进行分组吗?我在想类似的东西
let price (k s t r v as foo) =
let d1 = d1 foo
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢.
您可以w, x, y, z通过类似的高阶函数有效地批处理您的参数(调用它们)
let batchedArgs f = f w x y z
Run Code Online (Sandbox Code Playgroud)
现在batchedArgs是对原始函数参数的闭包.您只需传递另一个具有相同数量/类型参数的函数,它们将被应用.
// other functions which you wish to pass the args to
let sub1 w x y z = 42
let sub2 w x y z = true
// main routine
let doStuff w x y z =
// one-time declaration of batching function is
// the only time you need to list out the arguments
let batchedArgs f = f w x y z
// from then on, invoke like this
batchedArgs sub1
// or like this, which looks more like a traditional function call
sub2 |> batchedArgs
Run Code Online (Sandbox Code Playgroud)