From 842e367ccd6d6304472dc4ce4e0a81e647d06cbd Mon Sep 17 00:00:00 2001 From: rxi Date: Sun, 2 Mar 2014 00:09:24 +0000 Subject: [PATCH] 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() --- README.md | 19 ++++++++++--------- lume.lua | 24 +++++++++++++----------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b53f101..5b8b965 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lume.lua b/lume.lua index a61a774..274f7dd 100644 --- a/lume.lua +++ b/lume.lua @@ -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