works:programmer:lua:linq
Некотороый аналог LINQ для Lua
function T(t) return setmetatable(t, {__index = table}) end local function lambda(query) return assert(loadstring("return function (x) return " .. query .. " end"))() end function table.where(tbl, query) query = lambda(query) local _new = T{} for _it, a in pairs(tbl) do if query(a) then _new:insert(a) end end return _new end function table.print(tbl) print(tbl) for _it, a in pairs(tbl) do print(a) end end a = T{ 1, 2, 3, "asd" } a:insert('seven') a:print() a:where('type(x) == "number"') :where('x > 1') :where('x ~= 3') :print()
works/programmer/lua/linq.txt · Последнее изменение: 2020/02/12 15:03 — 127.0.0.1