Changed lume.fn/once's returned func return, v1.0.8

* The functions which lume.fn() and lume.once() return now return their
  inner functions' return value instead of nil
* Moved lume.once() in lume.lua and README.md to be near its similar
  functions -- lume.fn() and lume.time()
This commit is contained in:
rxi 2014-03-02 00:09:24 +00:00
parent d65556bc83
commit 842e367ccd
2 changed files with 23 additions and 20 deletions

View File

@ -121,15 +121,6 @@ exist in the table.
lume.find({"a", "b", "c"}, "b") -- Returns 2
```
### lume.once(fn, ...)
Returns a wrapper function to `fn` which takes the supplied arguments. The
wrapper function will call `fn` on the first call and do nothing on any
subsequent calls.
```lua
local f = lume.once(print, "Hello")
f() -- Prints "Hello"
f() -- Does nothing
```
### lume.slice(t [, i [, j]])
Mimics the behaviour of Lua's `string.sub`, but operates on an array rather
@ -149,6 +140,16 @@ local f = lume.fn(print, "Hello")
f() -- Prints "Hello"
```
### lume.once(fn, ...)
Returns a wrapper function to `fn` which takes the supplied arguments. The
wrapper function will call `fn` on the first call and do nothing on any
subsequent calls.
```lua
local f = lume.once(print, "Hello")
f() -- Prints "Hello"
f() -- Does nothing
```
### 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.

View File

@ -7,7 +7,7 @@
-- under the terms of the MIT license. See LICENSE for details.
--
local lume = { _version = "1.0.7" }
local lume = { _version = "1.0.8" }
function lume.clamp(x, min, max)
@ -144,15 +144,6 @@ function lume.find(t, value)
end
function lume.once(fn, ...)
local arg = {...}
return function()
if arg == nil then return end
fn(unpack(arg))
arg = nil
end
end
function lume.slice(t, i, j)
i = i or 1
@ -174,7 +165,18 @@ end
function lume.fn(fn, ...)
local arg = {...}
return function() fn(unpack(arg)) end
return function() return fn(unpack(arg)) end
end
function lume.once(fn, ...)
local arg = {...}
return function()
if arg == nil then return end
local rtn = {fn(unpack(arg))}
arg = nil
return unpack(rtn)
end
end