From b873c043c5f94317573991fa7a07588875d45ad1 Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 10 Jan 2015 16:29:13 +0000 Subject: [PATCH] Added lume.call(), updated tests and README --- README.md | 8 ++++++++ lume.lua | 7 +++++++ test/test_lume.lua | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/README.md b/README.md index adf1d30..df7c923 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,14 @@ local f = lume.combine(function(a, b) print(a + b) end, f(3, 4) -- Prints "7" then "12" on a new line ``` +### lume.call(fn, ...) +Calls the given function with the provided arguments and returns its value. If +`fn` is nil then no action is performed and the function returns nil. +```lua +_.call(print, "Hello world") -- Prints "Hello world" +``` + + ### 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 66eefb6..c1c557a 100644 --- a/lume.lua +++ b/lume.lua @@ -377,6 +377,13 @@ function lume.combine(...) end +function lume.call(fn, ...) + if fn then + return fn(...) + end +end + + function lume.time(fn, ...) local start = os.clock() local rtn = {fn(...)} diff --git a/test/test_lume.lua b/test/test_lume.lua index 66db5f6..7979dbe 100644 --- a/test/test_lume.lua +++ b/test/test_lume.lua @@ -351,6 +351,14 @@ tests["lume.combine"] = function() testeq( acc, 230 ) end +-- lume.call +tests["lume.call"] = function() + local add = function(a, b) return a + b end + testeq( lume.call(), nil ) + testeq( lume.call(nil, 1, 2, 3), nil ) + testeq( lume.call(add, 1, 2), 3 ) +end + -- lume.time tests["lume.time"] = function() local t, a, b, c = lume.time(function(x) return 50, 60, x end, 70)