如何用一个参数来讨论方法

时间:2022-11-12 21:25:17

here is my code:

这是我的代码:

def f x
  x
end

g = method(:f).to_proc.curry.(123)

p g

I want g to be a callable that takes no parameters and applies 123 to f. Instead, g contains the result of the application.

我希望g是一个不带参数的callable,并将123应用于f。相反,g包含应用程序的结果。

Am I doing it the complicated way?

我这么复杂吗?

EDIT: yes, g = lambda {f 123} works, but I am asking how to curry f.

编辑:是的,g = lambda {f 123}有效,但我问如何咖喱f。

2 个解决方案

#1


4  

The documentation for curry says that

咖喱的文件说明了这一点

If a sufficient number of arguments are supplied, it passes the supplied arguments to the original proc and returns the result.

如果提供了足够数量的参数,它会将提供的参数传递给原始proc并返回结果。

So in this case you haven't really curried your function from a theoretical point of view, but practically you have. The

所以在这种情况下,从理论的角度来看,你并没有真正理解你的功能,但实际上你已经有了。该

g = lambda {f 123}

seems to be closer to the spirit of returning a function that you can then call to evaluate, at least once all the arguments are determined.

似乎更接近返回一个函数的精神,然后你可以调用它来评估,至少在确定所有参数之后。

#2


-1  

Maybe you want to wrap your function f inside a lambda (evaluating f). Then you can curry the lambda expression, something like this:

也许你想把你的函数f包装在lambda中(评估f)。然后你可以讨论lambda表达式,如下所示:

g = lambda{f 123}.curry
p g[] // or g.call

Now g is callable that takes no parameters.

现在g是可调用的,不带参数。

#1


4  

The documentation for curry says that

咖喱的文件说明了这一点

If a sufficient number of arguments are supplied, it passes the supplied arguments to the original proc and returns the result.

如果提供了足够数量的参数,它会将提供的参数传递给原始proc并返回结果。

So in this case you haven't really curried your function from a theoretical point of view, but practically you have. The

所以在这种情况下,从理论的角度来看,你并没有真正理解你的功能,但实际上你已经有了。该

g = lambda {f 123}

seems to be closer to the spirit of returning a function that you can then call to evaluate, at least once all the arguments are determined.

似乎更接近返回一个函数的精神,然后你可以调用它来评估,至少在确定所有参数之后。

#2


-1  

Maybe you want to wrap your function f inside a lambda (evaluating f). Then you can curry the lambda expression, something like this:

也许你想把你的函数f包装在lambda中(评估f)。然后你可以讨论lambda表达式,如下所示:

g = lambda{f 123}.curry
p g[] // or g.call

Now g is callable that takes no parameters.

现在g是可调用的,不带参数。