【发布时间】:2014-11-27 12:27:58
【问题描述】:
我刚开始学习lua,所以我问的可能是不可能的。
现在,我有一个接受函数的方法:
function adjust_focused_window(fn)
local win = window.focusedwindow()
local winframe = win:frame()
local screenrect = win:screen():frame()
local f, s = fn(winframe, screenrect)
win:setframe(f)
end
我有几个函数可以接受这些框架和矩形(仅显示一个):
function full_height(winframe, screenrect)
print ("called full_height for " .. tostring(winframe))
local f = {
x = winframe.x,
y = screenrect.y,
w = winframe.w,
h = screenrect.h,
}
return f, screenrect
end
然后,我可以执行以下操作:
hotkey.bind(scmdalt, '-', function() adjust_focused_window(full_width) end)
现在,我如何在不更改其定义的情况下将多个函数组合成adjust_focused_window。比如:
hotkey.bind(scmdalt, '=', function() adjust_focused_window(compose(full_width, full_height)) end)
其中compose2 将返回一个函数,该函数接受与full_width 和full_height 相同的参数,并在内部执行如下操作:
full_height(full_width(...))
【问题讨论】:
-
function compose2(f1, f2) return function(...) return f1(f2(...)) end end
-
谢谢@moteus,这就像我的魅力。我必须更改 full_height/width 返回的内容才能将它们链接起来(现在反映在我的问题中)。
-
问题=~s/可能不可能/可能很简单,我应该多学习/;
-
@moteus 如果它对他有用,你能把它作为答案吗