Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local linqtable = {
- -- Methode Add --
- add = function( self, item )
- if self ~= nil and type(self) == "table" and item ~= nil then
- table.insert(self, item)
- return self
- else
- errorLqT("bad methodes calling, use 'LQt:add(item)' and not 'LQt.add(item)' ")
- end
- end,
- addToIndex = function( self, item, index )
- if self ~= nil and type(self) == "table" and item ~= nil and index ~= nil then
- if type(index) == "number" then
- table.insert(self,index,item)
- else
- errorLqT("addToIndex : index is not a number")
- end
- return self
- else
- errorLqT("bad methodes calling, use 'LQt:addToIndex(item,index)' and not 'LQt.addToIndex(item,index)' ")
- end
- end,
- concat = function( self, tableAdd )
- if self ~= nil and type(self) == "table" and tableAdd ~= nil then
- if type(tableAdd) == "table" then
- for key,value in pairs(tableAdd) do
- self:add(value)
- end
- else
- errorLqT("concat : tableAdd is not a table")
- end
- return self
- else
- errorLqT("bad methodes calling, use 'LQt:concat(tableAdd)' and not 'LQt.concat(tableAdd)' ")
- end
- end,
- -- Methode remove --
- removeLast = function( self )
- if self ~= nil and type(self) == "table" then
- table.remove(self)
- return self
- else
- errorLqT("bad methodes calling, use 'LQt:removeLast()' and not 'LQt.removeLast()' ")
- end
- end,
- removeToIndex = function( self, index )
- if self ~= nil and type(self) == "table" and index ~= nil then
- if type(index) == "number" then
- table.remove(self, index)
- else
- errorLqT("removeToIndex : index is not a number")
- end
- return self
- else
- errorLqT("bad methodes calling, use 'LQt:removeToIndex(index)' and not 'LQt.removeToIndex(index)' ")
- end
- end,
- -- utility basic --
- length = function( self )
- if self ~= nil and type(self) == "table" then
- return #self
- else
- errorLqT("bad methodes calling, use 'LQt:length()' and not 'LQt.length()' ")
- end
- end,
- tostring = function( self )
- if self ~= nil and type(self) == "table" then
- local toSender = "LinqTable :\r\n{"
- for key,value in pairs(self) do
- toSender = toSender.."\t"
- if type(value) == "table" then
- toSender = toSender.."table"
- elseif type(value) == "function" then
- toSender = toSender.."function"
- else
- toSender = toSender..value
- end
- toSender = toSender..",\r\n"
- end
- toSender = toSender.."}"
- return toSender
- else
- errorLqT("bad methodes calling, use 'LQt:tostring()' and not 'LQt.tostring()' ")
- end
- end,
- -- utility Linq --
- forEach = function( self, lambda )
- if self ~= nil and type(self) == "table" and lambda ~= nil then
- if type(lambda) == "function" then
- for key,value in pairs(self) do
- lambda(value,key)
- end
- else
- errorLqT("forEach : lambda is not a function")
- end
- return self
- else
- errorLqT("bad methodes calling, use 'LQt:forEach(lambda)' and not 'LQt.forEach(lambda)' ")
- end
- end,
- select = function( self, lambda )
- if self ~= nil and type(self) == "table" and lambda ~= nil then
- if type(lambda) == "function" then
- local toSender = new({});
- for key,value in pairs(self) do
- local resul = lambda(value,key)
- if resul == nil then
- errorLqT("select : lambda can't retun nil")
- end
- toSender:add(resul)
- end
- return toSender
- else
- errorLqT("select : lambda is not a function")
- end
- else
- errorLqT("bad methodes calling, use 'LQt:select(lambda)' and not 'LQt.select(lambda)' ")
- end
- end,
- where = function( self, lambda )
- if self ~= nil and type(self) == "table" and lambda ~= nil then
- if type(lambda) == "function" then
- local toSender = new({});
- for key,value in pairs(self) do
- local resul = lambda(value,key)
- if resul == nil then
- errorLqT("where : lambda can't retun nil")
- elseif resul == true then
- toSender:add(resul)
- end
- end
- return toSender
- else
- errorLqT("where : lambda is not a function")
- end
- else
- errorLqT("bad methodes calling, use 'LQt:where(lambda)' and not 'LQt.where(lambda)' ")
- end
- end,
- first = function( self, lambda )
- if self ~= nil and type(self) == "table" then
- if type(lambda) ~= "function" then
- lambda = true
- end
- for key,value in pairs(self) do
- local resul = lambda(value,key)
- if resul == true then
- return value
- end
- end
- return nil
- else
- errorLqT("bad methodes calling, use 'LQt:first()' or 'LQt:first(lambda)' and not 'LQt.first(lambda)' ")
- end
- end,
- sort = function( self, lambda )
- if self ~= nil and type(self) == "table" then
- if type(lambda) ~= "function" then
- table.sort(self, lambda )
- else
- table.sort(self)
- end
- else
- errorLqT("bad methodes calling, use 'LQt:sort()' or 'LQt:sort(lambda)' and not 'LQt.sort(lambda)' ")
- end
- end,
- }
- local LinqTableMetatable = {
- __index = linqtable,
- __add = linqtable.add,
- __tostring = linqtable.tostring,
- }
- --------- Constructor ---------
- function new(Tableau)
- setmetatable(Tableau, LinqTableMetatable)
- return Tableau
- end
- local function errorLqT(message)
- error("LinqTable - "..message)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement