mirror of
https://github.com/TangentFoxy/lume.git
synced 2024-11-19 07:04:24 +00:00
Added lume.memoize(), updated README.md and tests
This commit is contained in:
parent
6a160a3afe
commit
0716caf6a1
@ -208,6 +208,14 @@ f() -- Prints "Hello"
|
||||
f() -- Does nothing
|
||||
```
|
||||
|
||||
### lume.memoize(fn)
|
||||
Returns a wrapper function to `fn` where the results for any given set of
|
||||
arguments are cached. `lume.memoize()` is useful when used on functions with
|
||||
slow-running computations.
|
||||
```lua
|
||||
fib = lume.memoize(function(n) return n < 2 and n or fib(n-1) + fib(n-2) end)
|
||||
```
|
||||
|
||||
### lume.time(fn, ...)
|
||||
Inserts the arguments into function `fn` and calls it. Returns the time in
|
||||
seconds the function `fn` took to execute followed by `fn`'s returned values.
|
||||
|
18
lume.lua
18
lume.lua
@ -269,6 +269,24 @@ function lume.once(fn, ...)
|
||||
end
|
||||
|
||||
|
||||
local memoize_fnkey = {}
|
||||
local memoize_nilkey = {}
|
||||
|
||||
function lume.memoize(fn)
|
||||
local cache = {}
|
||||
return function(...)
|
||||
local c = cache
|
||||
for i = 1, select("#", ...) do
|
||||
local a = select(i, ...) or memoize_nilkey
|
||||
c[a] = c[a] or {}
|
||||
c = c[a]
|
||||
end
|
||||
c[memoize_fnkey] = c[memoize_fnkey] or {fn(...)}
|
||||
return unpack(c[memoize_fnkey])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function lume.time(fn, ...)
|
||||
local start = os.clock()
|
||||
local rtn = {fn(...)}
|
||||
|
@ -279,6 +279,20 @@ tests["lume.once"] = function()
|
||||
tester.test.error( lume.once, 123 )
|
||||
end
|
||||
|
||||
-- lume.memoize
|
||||
tests["lume.memoize"] = function()
|
||||
local f = lume.memoize(
|
||||
function(a, b, c)
|
||||
return tostring(a) .. tostring(b) .. tostring(c)
|
||||
end)
|
||||
testeq( f("hello", nil, 15), "hellonil15" )
|
||||
testeq( f("hello", nil, 15), "hellonil15" )
|
||||
testeq( f(), "nilnilnil" )
|
||||
testeq( f(), "nilnilnil" )
|
||||
local f2 = lume.memoize(function() end)
|
||||
testeq( f2(), nil )
|
||||
end
|
||||
|
||||
-- lume.time
|
||||
tests["lume.time"] = function()
|
||||
local t, a, b, c = lume.time(function(x) return 50, 60, x end, 70)
|
||||
|
Loading…
Reference in New Issue
Block a user