Advertisement
lord_thekenny

linqtable

Nov 11th, 2021
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. local linqtable = {
  2.  
  3.     -- Methode Add --
  4.     add = function( self, item )
  5.         if self ~= nil and type(self) == "table" and item ~= nil then
  6.             table.insert(self, item)
  7.             return self
  8.         else
  9.             errorLqT("bad methodes calling, use 'LQt:add(item)' and not 'LQt.add(item)' ")
  10.         end
  11.     end,
  12.     addToIndex = function( self, item, index )
  13.         if self ~= nil and type(self) == "table" and item ~= nil and index ~= nil then
  14.             if type(index) == "number" then
  15.                 table.insert(self,index,item)
  16.             else
  17.                 errorLqT("addToIndex : index is not a number")
  18.             end
  19.             return self
  20.         else
  21.             errorLqT("bad methodes calling, use 'LQt:addToIndex(item,index)' and not 'LQt.addToIndex(item,index)' ")
  22.         end
  23.     end,
  24.     concat = function( self, tableAdd )
  25.         if self ~= nil and type(self) == "table" and tableAdd ~= nil then
  26.             if type(tableAdd) == "table" then
  27.                 for key,value in pairs(tableAdd) do
  28.                     self:add(value)
  29.                 end
  30.             else
  31.                 errorLqT("concat : tableAdd is not a table")
  32.             end
  33.             return self
  34.         else
  35.             errorLqT("bad methodes calling, use 'LQt:concat(tableAdd)' and not 'LQt.concat(tableAdd)' ")
  36.         end
  37.     end,
  38.  
  39.     -- Methode remove --
  40.     removeLast = function( self )
  41.         if self ~= nil and type(self) == "table" then
  42.             table.remove(self)
  43.             return self
  44.         else
  45.             errorLqT("bad methodes calling, use 'LQt:removeLast()' and not 'LQt.removeLast()' ")
  46.         end
  47.     end,
  48.     removeToIndex = function( self, index )
  49.         if self ~= nil and type(self) == "table" and index ~= nil then
  50.             if type(index) == "number" then
  51.                 table.remove(self, index)
  52.             else
  53.                 errorLqT("removeToIndex : index is not a number")
  54.             end
  55.             return self
  56.         else
  57.             errorLqT("bad methodes calling, use 'LQt:removeToIndex(index)' and not 'LQt.removeToIndex(index)' ")
  58.         end
  59.     end,
  60.  
  61.     -- utility basic --
  62.     length = function( self )
  63.         if self ~= nil and type(self) == "table" then
  64.             return #self
  65.         else
  66.             errorLqT("bad methodes calling, use 'LQt:length()' and not 'LQt.length()' ")
  67.         end
  68.     end,
  69.     tostring = function( self )
  70.         if self ~= nil and type(self) == "table" then
  71.             local toSender = "LinqTable :\r\n{"
  72.             for key,value in pairs(self) do
  73.                 toSender = toSender.."\t"
  74.                 if type(value) == "table" then
  75.                     toSender = toSender.."table"
  76.                 elseif type(value) == "function" then
  77.                     toSender = toSender.."function"
  78.                 else
  79.                     toSender = toSender..value
  80.                 end
  81.                 toSender = toSender..",\r\n"
  82.             end
  83.             toSender = toSender.."}"
  84.             return toSender
  85.         else
  86.             errorLqT("bad methodes calling, use 'LQt:tostring()' and not 'LQt.tostring()' ")
  87.         end
  88.     end,
  89.  
  90.     -- utility Linq --
  91.     forEach = function( self, lambda )
  92.         if self ~= nil and type(self) == "table" and lambda ~= nil then
  93.             if type(lambda) == "function" then
  94.                 for key,value in pairs(self) do
  95.                     lambda(value,key)
  96.                 end
  97.             else
  98.                 errorLqT("forEach : lambda is not a function")
  99.             end
  100.             return self
  101.         else
  102.             errorLqT("bad methodes calling, use 'LQt:forEach(lambda)' and not 'LQt.forEach(lambda)' ")
  103.         end
  104.     end,
  105.     select = function( self, lambda )
  106.         if self ~= nil and type(self) == "table" and lambda ~= nil then
  107.             if type(lambda) == "function" then
  108.                 local toSender = new({});
  109.                 for key,value in pairs(self) do
  110.                     local resul = lambda(value,key)
  111.                     if resul == nil then
  112.                         errorLqT("select : lambda can't retun nil")
  113.                     end
  114.                     toSender:add(resul)
  115.                 end
  116.                 return toSender
  117.             else
  118.                 errorLqT("select : lambda is not a function")
  119.             end
  120.         else
  121.             errorLqT("bad methodes calling, use 'LQt:select(lambda)' and not 'LQt.select(lambda)' ")
  122.         end
  123.     end,
  124.     where = function( self, lambda )
  125.         if self ~= nil and type(self) == "table" and lambda ~= nil then
  126.             if type(lambda) == "function" then
  127.                 local toSender = new({});
  128.                 for key,value in pairs(self) do
  129.                     local resul = lambda(value,key)
  130.                     if resul == nil then
  131.                         errorLqT("where : lambda can't retun nil")
  132.                     elseif resul == true then
  133.                         toSender:add(resul)
  134.                     end
  135.                 end
  136.                 return toSender
  137.             else
  138.                 errorLqT("where : lambda is not a function")
  139.             end
  140.         else
  141.             errorLqT("bad methodes calling, use 'LQt:where(lambda)' and not 'LQt.where(lambda)' ")
  142.         end
  143.     end,
  144.     first = function( self, lambda )
  145.         if self ~= nil and type(self) == "table" then
  146.             if type(lambda) ~= "function" then
  147.                 lambda = true
  148.             end
  149.             for key,value in pairs(self) do
  150.                 local resul = lambda(value,key)
  151.                 if resul == true then
  152.                     return value
  153.                 end
  154.             end
  155.             return nil
  156.  
  157.         else
  158.             errorLqT("bad methodes calling, use 'LQt:first()' or 'LQt:first(lambda)' and not 'LQt.first(lambda)' ")
  159.         end
  160.     end,
  161.     sort = function( self, lambda )
  162.         if self ~= nil and type(self) == "table" then
  163.             if type(lambda) ~= "function" then
  164.                 table.sort(self, lambda )
  165.             else
  166.                 table.sort(self)
  167.             end
  168.         else
  169.             errorLqT("bad methodes calling, use 'LQt:sort()' or 'LQt:sort(lambda)' and not 'LQt.sort(lambda)' ")
  170.         end
  171.     end,
  172. }
  173.  
  174. local LinqTableMetatable = {
  175.     __index = linqtable,
  176.     __add = linqtable.add,
  177.     __tostring = linqtable.tostring,
  178. }
  179.  
  180. --------- Constructor ---------
  181. function new(Tableau)
  182.     setmetatable(Tableau, LinqTableMetatable)
  183.     return Tableau
  184. end
  185.  
  186. local function errorLqT(message)
  187.     error("LinqTable - "..message)
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement