在F#中包装带有不确定数量的参数的函数 [英] Wrapping a function with an indeterminate number of parameters in F#
问题描述
我正在尝试用F#语言编写一个简单的包装器类,该类使用一个返回字符串的函数,并返回一个具有相同参数的函数,并从输入的包装"中返回字符串.
I'm trying to write a simple wrapper class in F# that takes a function that returns a string, and returns a function that takes the same parameters and returns the string from the input 'wrapped'.
以下代码适用于带有单个变量的函数(因此test
可以正常工作):
The following code works for functions that take a single variable (so test
works fine):
open System
let myFunc anotherFunc =
fun x -> "boo" + anotherFunc x + "unboo"
let Func2 toDouble =
(toDouble * 2).ToString ()
let test = myFunc Func2
let Func3 numOne numTwo =
(numOne * numTwo).ToString ()
let test2 = myFunc Func3
do
Console.WriteLine(test 10)
Console.WriteLine(test2 5 10)
但是因为fun x ->
指定单个参数,所以test2
不是有效的代码.有没有一种语法可以允许这种构造?
But because fun x ->
specifies a single parameter, test2
is not valid code. Is there a piece of syntax that would allow for this construct?
推荐答案
您的函数有效. 问题出在最后一行上
Your function works. The problem is in your last line when you do
let test2 = myFunc Func3
应该是
let test2 x = myFunc (Func3 x)
更新:
我假设您不想更改Func3.否则,您可能需要Tomas的解决方案.
I'm assuming you don't want to change Func3. Otherwise the solution from Tomas could be what you need.
这篇关于在F#中包装带有不确定数量的参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!