温馨提示:本文翻译自stackoverflow.com,查看原文请点击:lua - Passing a function with some arguments already filled
arguments function lua

lua - 通过已填充一些参数的函数

发布于 2020-04-19 11:46:14

我想将一个函数作为参数传递,要传递的函数需要两个参数。我想要第一个参数填充,但第二个参数不填充。这是示例:

function a(firstarg, secondarg)
    print ("this is the" .. firstarg .. "to the a function and the b function gave it ".. secondarg)
end

function b(givenfunction)
    givenfunction("the second argument.")

对该函数的所需调用:

b(a("first call"))
b(a("second call"))

执行的期望输出:

this is the first call to the a function and the b function gave it the second argument. 
this is the second call to the a function and the b function gave it the second argument.

我怎样才能做到这一点?

查看更多

提问者
Alexandre Willame
被浏览
78
Egor Skriptunoff 2020-02-05 02:04
function inner(firstarg, secondarg)
   print ("this is the" .. firstarg .. "to the a function and the b function gave it ".. secondarg)
end

function a(firstarg)
   return function (secondarg) 
      return inner(firstarg, secondarg)
   end
end

function b(givenfunction)
   givenfunction("the second argument.")
end

b(a("first call"))
b(a("second call"))