vim.func
Methods1
function M._memoize<F>(hash: function | string | integer, fn: F, weak: nil | boolean) -> F
TODO(lewis6991): Private for now until:
- There are other places in the codebase that could benefit from this
- I don't think the story around
hashis completely thought out. We - Need to support multi level caches. Can be done by allow
hashto
(e.g. LSP), but might require other changes to accommodate.
may be able to have a good default hash by hashing each argument, so basically a better 'concat'.
return multiple values.
Memoizes a function {fn} using {hash} to hash the arguments.
Internally uses a |lua-weaktable| to cache the results of {fn} meaning the cache will be invalidated whenever Lua does garbage collection.
The cache can also be manually invalidated by calling :clear() on the returned object. Calling this function with no arguments clears the entire cache; otherwise, the arguments will be interpreted as function inputs, and only the cache entry at their hash will be cleared.
The memoized function returns shared references so be wary about mutating return values.
hashfunction | string | integerHash function to create a hash to use as a key to store results. Possible values:
- When integer, refers to the index of a {fn} argument (of any type) to hash.
- When function, is evaluated using the same arguments passed to {fn}.
- When "concat", hash is determined by string-concatenating all {fn} arguments.
- When "concat-n", hash is determined by string-concatenating the first n {fn} arguments.
fnFFunction to memoize.
weaknil | booleanUse a weak table (default
true)
- F
Memoized version of {fn}