commit 0132933: [Minor] Lua_util: Add `flatten` function

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Jul 23 17:07:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-07-23 17:51:00 +0100
URL: https://github.com/rspamd/rspamd/commit/01329335b7941502da10c71ead38b9c334804c84

[Minor] Lua_util: Add `flatten` function

---
 lualib/lua_util.lua | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index 432f55c6a..b8420c7a4 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -284,6 +284,23 @@ exports.unpack = function(t)
   return unpack_function(t)
 end
 
+--[[[
+-- @function lua_util.flatten(table)
+-- Flatten underlying tables in a single table
+-- @param {table} table table of tables
+-- @return {table} flattened table
+--]]
+exports.flatten = function(t)
+  local res = {}
+  for _,e in fun.iter(t) do
+    for _,v in fun.iter(e) do
+      res[#res + 1] = v
+    end
+  end
+
+  return res
+end
+
 --[[[
 -- @function lua_util.spairs(table)
 -- Like `pairs` but keys are sorted lexicographically


More information about the Commits mailing list