Skip to content

Commit 6ced4d3

Browse files
add memoization first draft/idea
1 parent 4dc7012 commit 6ced4d3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lua/luasnip/extras/expand_conditions.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,36 @@ function M.line_end(line_to_cursor)
1010
return #line_to_cursor == #line
1111
end
1212

13+
local memoization_mt = {
14+
__unm = function(o1) return function() return not o1() end end,
15+
__add = function(o1,o2) return function() return o1() or o2() end end,
16+
__mul = function(o1,o2) return function() return o1() and o2() end end,
17+
-- TODO more logic operators
18+
__call = function(tab)
19+
if not tab.mem or tab.invalidate(tab) then
20+
tab.mem = tab.func()
21+
end
22+
return tab.mem
23+
end
24+
}
25+
-- low level factory
26+
-- invalidate(table) -> bool: decides if the memoization should be invalidated,
27+
-- can store state in table
28+
-- TODO provide invalidate defaults (buffer, cursor, changes, none)
29+
function M.memoization_factory(func, invalidate)
30+
-- always invalidare by default
31+
invalidate = invalidate or function() return true end
32+
return setmetatable({func=func, invalidate=invalidate}, memoization_mt)
33+
end
34+
35+
-- F1 = memoization_factory(function() return true end)
36+
-- F2 = memoization_factory(function() return false end)
37+
-- F3 = F1 + F2
38+
-- F4 = F1 * -F2
39+
--
40+
-- local m = {F1=F1, F2=F2, F3=F3, F4=F4}
41+
-- for _,name in ipairs{"F1", "F2", "F3", "F4"} do
42+
-- print(name, m[name]())
43+
-- end
44+
1345
return M

0 commit comments

Comments
 (0)