Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 11 Jun 2015 02:15 AM |
I'm making an extended table library, and want suggestions.
I currently have;
table.lock table.unlock table.clone table.fill
Please make suggestions that include how the calling would work and a sufficient explanation of what said function will do.
Thanks. |
|
|
| Report Abuse |
|
drager980
|
  |
| Joined: 25 May 2009 |
| Total Posts: 13385 |
|
|
| 11 Jun 2015 02:41 AM |
table.merge?
local a = {"q"} local b= {"e"} print(table.concat(table.merge(a,b)," "))
AND THE TIGER GOES ROAR |
|
|
| Report Abuse |
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 11 Jun 2015 03:18 AM |
pshhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
I can make a better one than that, he's not even using metatables.
I can steal quite a few function ideas from there but feel free to still post suggestions c: |
|
|
| Report Abuse |
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 11 Jun 2015 07:05 AM |
Functions from here: https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-List.html
Important ones: head/last/tail/init :: [a] -> [a] map :: (a -> b) -> [a] -> [b]
foldl :: (Foldable t) => (b -> a -> b) -> b -> t a -> b foldr :: (Foldable t) => (a -> b -> b) -> b -> t a -> b scanl :: (b -> a -> b) -> b -> [a] -> [b] scanr :: (b -> a -> b) -> b -> [a] -> [b]
and/or :: (Foldable t) => t Bool -> Bool any/all :: (Foldable t) => (a -> Bool) -> t a -> Bool sum/product :: (Foldable t, Num a) => t a -> a
-- The following are infinite in Haskell, would be slightly different in Lua iterate :: (a -> a) -> a -> [a] replicate :: Int -> a -> [a] cycle :: [a] -> [a]
take/drop :: Int -> [a] -> [a] takeWhile/dropWhile :: (a -> Bool) -> [a] -> [a]
-- This are probably better for strings but in Haskell, type String = [Char] isPrefixOf/isSuffixOf/isInfixOf :: (Eq a) => [a] -> [a] -> Bool
elem :: (Foldable t, Eq a) => a -> t a -> Bool find :: (Foldable t) => (a -> Bool) -> t a -> Maybe a filter :: (a -> Bool) -> [a] -> [a] findIndex :: (a -> Bool) -> [a] -> Maybe Int
zip :: [a] -> [b] -> [(a, b)] zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] unzip :: [(a, b)] -> ([a], [b])
union/intersect :: (Eq a) => [a] -> [a] -> [a]
|
|
|
| Report Abuse |
|
|
| 11 Jun 2015 03:08 PM |
I have some, I've already pre-written the docs for you :)
(Yes, this is every single function in the library you are copying, don't worry, I don't mind :D)
~~3. Functions and their Usage~~ -3.1 table.copy table.copy is a very useful function, yet complicated function. Don't fret, it's usage is easy to learn. table table.copy(table Table, bool DeepCopy = false, int Metatable = void) table.copy creates a copy of a table, since tables are references it can be hard. table.copy makes it easy, by allowing you to simply call a function. It's arguments are: Table; The table it operates on DeepCopy; Weather it should continue copying tables inside, or store references. (true will result in a DeepCopy) Metatable; Weather it should copy the metatable or not. (1 for copy, 2 for reference, 3 for empty metatable) Note, this will not Metatable/DeepCopy the metatable. -3.2 table.destroy table.destroy is intended to replace table.remove, it has easy usage. table table.destroy(table tab, any val) table.destroy simply destroys a value in a table. Arguments are listed as follows: tab; The table it operates on. val; The value is destroys. table.destroy will eliminate all copies of val in the table! -3.3 table.pack table.pack is the inverse of unpack, usage is extremely easy to learn! table table.pack(any ...) It's only argument is ... which can be any number of values, but afterwards it will all be in one single table for your scripts! -3.4 table.clear table.clear has only one purpose. To ultimately destroy all values in your table! table table.clear(table tab) tab; The table you are operating on -3.5 table.length table.length gets the length of a table. int table.length(table tab) tab; The table you are operating on Why is this useful? Sometimes the # operator don't cut it. Example: print(#{five = "5", nine = "9"}) That would print 0. table.length prints 2. -3.6 table.reverse table.reverse reverses all the values in your table. table table.reverse(table tab) tab; The table you are reversing -3.7 table.flatten table.flatten, flattens your table! table table.flatten(table tab) tab; The table you are operating on What happens when you flatten table? Here's an example; {5, 6, {9, 5, {9, 3}, {3}, 6}, {3}} A lot of tables within that table, after we flatten it we'll get {5, 6, 9, 5, 9, 3, 3, 6, 3} -3.8 table.find Here's a useful one, table.find searches through your table to find a value of your choosing, then returns if it found it, what it found, and where it found it. bool, any, int table.find(table tab, any val, bool deep = false) The arguments are pretty easy: tab; The table you are searching through. val; The value you are searching for. deep; If you wish to search further down through more tables. -3.9 table.contents Here's one for if you plan on searching down through a table heirachy. Usually for debugging. table table.contents(table T, bool depth = false, bool Print = false, int lvl = 1, table Copies = {}) The last 2 arguments you don't really have to worry about, they're just for advanced debuggers. T; The table your are dumping the contents from. depth; Just like deep from earlier functions. This simply recurses to further tables. Print; Weather or not you want to print the values or not. This returns the Copies table, which you do not need to worry about. Just do not assign what this returns to your table! -3.10 table.slice This one is pretty useful. He slices out a section of your table table table.slice(table tab, int start, int finish = #tab) tab; The table you are operating upon. start; The start index of where you are slicing. finish; The finish index of where you are slicing. Defaults to the end of table. -3.11 table.dimensional This one turns a 1 dimensional array into multidimensional array. You can make it 2D, 3D, or 4D. You cannot transfer back to 1D with this function, (See table.flatten). table table.dimensional(table tab, int dimensions) tab; The table you are operating upon dimensions; How many dimensions you want the new table to be -3.12 table.tostring This function is almost identical to table.concat, except it tostrings everything (this way there {}, functions, and userdatas do not return as invalid) string table.tostring(table tab) tab; The table you are operating upon -3.13 table.compare This function compares 2 tables to eachother, using key, value pairs. Note; this function will not deep search. If it finds a table inside it does not compare that table to the table in the other table using key, value pairs. ( It comapres them by reference) bool table.compare(table tab1, table tab2, bool meta = false) tab1; The table you are comparing to tab2 tab2; The table you are comparing to tab1 meta; Whether the metas should be compared in the same way -3.14 table.join This function takes 2 tables, joins them to eachother, and then returns the new table table table.join(table tab1, table tab2 [any extras]) tab1; The first indexes of the new table tab2; The last indexes of the new table extras; Optional values to insert to the end of the new table -3.15 table.depth This function takes a table and returns how deep it reaches in hierarchy int table.depth(table tab) tab; The table you are operating on -3.16 table.fill This function fills in all the nil values in a numeric array with a value you specify void table.fill(table tab, val) tab; The table you are operating on val; The value you are using to fill -3.17 table.shuffle This function takes a table and shuffles all the values inside table table.shuffle(table tab) tab; The table you are operating on -3.18 table.random This function returns a random value inside the table. The table can be a dictionary! any table.random(table tab, int am = 1, returnTable = void) tab; The table you are operating on am; The amount of random values to return returnTable; Whether to return the results as a table or not -3.19 table.results This function runs a function against every value in a table, then returns a table of results: table table.results(table tab, function func) tab; The table you are operating on func; The function you are running against each element -3.20 table.test This function runs a function against every value in a table, then returns true if the test succeeds. There are 2 types of tests "any" and "all". "any" succeeds if the function returns true at any point. "all" succeeds if the function always returns true bool table.test(table tab, function func, string type = "any") tab; The table you are operating on func; The function you are running type; The type of test you are running -3.21 table.take This function takes the first x elements of a table and returns them as a table. table table.take(table tab, int length) tab; The table you are operating on length; The length of the elements you are taking -3.22 table.drop This function takes all of the elements of a table after the first x and returns them as a table. table table.drop(table tab, int length, bool include = false) tab; The table you are operating on length; The length of the elemtns before you what are taking include; Whether to include the xth element of the table too -3.23 table.takeWhile This function runs a function against all of the elements of a table and returns a table of the values which returned true table table.takeWhile(table tab, function func) tab; The table you are operating on funcl The function you are running against each element -3.24 table.dropWhile This function runs a function against all of the elements of a table and returns a table of the values which returned false table table.dropWhile(table tab, function func) tab; The table you are operating on funcl The function you are running against each element -3.25 table.split This function splits a table into 2 tables at a given index, and then returns a table containing those 2 tables. table table.split(table tab, int index) tab; The table you are operating on index; The index you are splitting at -3.26 table.zip This function zips 2 tables together, it pairs the indexes of both tables into their own table, then returns a table containing all those tables. table table.zip(table tab1, table tab2) tab1; The table containing the first indexes tab2; The table containing the second indexes -3.27 table.replace This function searches through a table and replaces a specific value with another, with an optional recursive. table table.replace(table tab, any find, any new, bool recursive = false) tab; The table you are operating on find; The value you are changing new; The value you are replacing it with recursive; If you want to replace within nested tables too -3.28 table.encrypt This function clears your table making it seem cleared until you decrypt it using table.decrypt. table table.encrypt(table tab, any key) tab; The table you are encrypting key; The key you are using to encrypt it with -3.29 table.decrypt This function decrypts your table and retuns it back to normal, normal being before it was encrypted using table.encrypt. table table.decrypt(table tab, any key) tab; The table you are decrypting key; The key you used to encrypt it, neseccary for decrypting -3.30 table.unpack This function works indentically to unpack, except also has the aditional deep argument for recursive unpack. multiple table.unpack(table tab, bool deep=false) tab; The table you are unpacking deep; Whether to unpack deeply/recursively -3.31 table.isArray This function checks if the table is an array, or if it's a dictionary. bool table.isArray(table tab) tab; The table you are checking -3.32 table.address This function returns the memory address of a table. string table.address(table tab) tab; The table you are getting the address of -3.33 table.count This function returns how many occurences there are of a value in the table int table.count(table tab, any val) tab; The table you are operating on val; The value you are counting -3.34 table.destroyFirst This function destroys the first instance of a value in a table (whereas table.destroy destroys them all) any table.destroyFirst(table tab, any val) tab; The table you are operating on val; The value you are counting -3.35 table.indexOf This function returns the index of a certain value, optionally which index it it finds. any table.indexOf(table tab, any val, int case=1) tab; The table you are operating on val; The value it's searching for case; Which index it finds to return (the 1st, the 2nd, the 3rd) -3.36 table.solidify This function takes a table and removes all "holes" (nil values) making it a solid array again. table table.solidify(table tab) tab; The table you are operating on 3.37 table.DestroyDuplicates Destroy all duplicates of a given value in the table, optional arguments allow you to set the begin index, and which occurrence to begin at after the begin index.
table table.DestroyDuplicates(table tab, any val, int start=1, occurrence=1)
tab; The table you are operating on val; The value to search duplicates for start; The start of the search occurrence; The occurrence after start to set as the original and begin from |
|
|
| Report Abuse |
|
|
| 11 Jun 2015 03:10 PM |
| btw, I don't use metatables for a specific reason, I don't want to potentially break metatables for the user, which could happen, besides, I want to stay consistent. No other functions mess with metatables. |
|
|
| Report Abuse |
|
|
| 11 Jun 2015 03:32 PM |
Wouldn't this be better for table.merge?
for i,v in pairs(table)do getfenv()[i] = v; end
local table = {};
table.merge = function(...) local Tables = {...}; local Result = {}; for i = 1, #Tables do if(type(Tables[i]) == 'table')then for ii = 1, #Tables[i] do insert(Result, Tables[i][ii]); end end end return Result; end
local Table = {'Hi'}; local Table2 = {'There'}; local Table3 = {'Scripters'};
local Table = table.merge(Table, Table2, Table3); print(unpack(Table)) |
|
|
| Report Abuse |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 11 Jun 2015 03:36 PM |
You guys are all going about this the wrong way, you wanna wrap the tables all with the same metatable (it won't interfere with the original table's meteatable) so you can do stuff like:
tbl:merge(tbl2):merge(tbl3) without having to create other new tables/functions for each new table |
|
|
| Report Abuse |
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 11 Jun 2015 03:39 PM |
Yes, this is every single function in the library you are copying, [2]
@Goul you kinda just got rekt |
|
|
| Report Abuse |
|