Lua - 传递一个已经填充了一些参数的函数

亚历山大·威廉

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

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.

我怎样才能做到这一点?

埃戈尔·斯克里普图诺夫
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"))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章