mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
17 lines
276 B
Lua
17 lines
276 B
Lua
local function memoize<X, Y>(f: (X) -> Y): (X) -> Y
|
|
local cache: { [X]: Y? } = {}
|
|
|
|
return function(x: X): Y
|
|
local y = cache[x]
|
|
|
|
if not y then
|
|
y = f(x)
|
|
cache[x] = y
|
|
end
|
|
|
|
return y :: Y
|
|
end
|
|
end
|
|
|
|
return memoize
|
|
|