mirror of
https://github.com/chenasraf/vim-matchup.git
synced 2026-05-18 01:38:57 +00:00
29 lines
682 B
Lua
29 lines
682 B
Lua
-- From https://github.com/nvim-treesitter/nvim-treesitter
|
|
-- Copyright 2021
|
|
-- licensed under the Apache License 2.0
|
|
-- See nvim-treesitter.LICENSE-APACHE-2.0
|
|
|
|
local M = {}
|
|
|
|
---Memoize a function using hash_fn to hash the arguments.
|
|
---@generic F: function
|
|
---@param fn F
|
|
---@param hash_fn fun(...): any
|
|
---@return F
|
|
function M.memoize(fn, hash_fn)
|
|
local cache = setmetatable({}, { __mode = 'kv' }) ---@type table<any,any>
|
|
|
|
return function(...)
|
|
local key = hash_fn(...)
|
|
if cache[key] == nil then
|
|
local v = fn(...) ---@type any
|
|
cache[key] = v ~= nil and v or vim.NIL
|
|
end
|
|
|
|
local v = cache[key]
|
|
return v ~= vim.NIL and v or nil
|
|
end
|
|
end
|
|
|
|
return M
|