|
| 20 May 2015 03:33 PM |
I'm sure many of you work with tables quite often, so you'd know what the table library is.
But in case you do not, I'm talking about the table called "table" (it contains things like table.insert, table.remove, table.concat)
I'm working on a module which will compliment that library, by adding more and more (useful) functions onto it. The table library is fairly limited, so if you guys had any ideas for new functions you'd like added onto it, I'd gladly give them a try.
I'm gonna go ahead and ask for no ridiculous obviously impossible ideas.
Here's a link to the module:
www.roblox.com/ExtendedTable-item?id=199207348
You don't actually need the module to require it:
local table = require(199207348)
Here is the current list of functions:
table.copy table.destroy table.pack table.clear table.length table.reverse table.flatten table.find table.contents table.slice table.dimensional table.tostring table.compare table.join table.depth table.fill
If you have any questions, feel free to ask :) |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
| |
|
|
| 20 May 2015 03:38 PM |
| @eLuante thanks, I'll add that later, it's relatively easy to make (I posted this because I'm going for a bit now and would hope to come back to a ton of suggestions) |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 20 May 2015 03:39 PM |
Add these: https://hackage.haskell.org/package/base-4.7.0.1/docs/Data-List.html |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
| |
|
ganger800
|
  |
| Joined: 06 Dec 2012 |
| Total Posts: 427 |
|
|
| 20 May 2015 03:44 PM |
how is the tostring compare-able to JSON? things like: time, result etc :) or is it more something to use to hash a table :) +add option to convert the string back to the table (if it is useful to do though) |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 04:47 PM |
| table.tostring is the same as table.concat except it tostrings are the elements inside so that you don't get invalid values if there are tables, functions, and userdatas inside. |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 05:02 PM |
@Dr01d
Could you be more specific, some of those (most actually) have no application in Lua... |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 05:39 PM |
table.remove() doesn't work with Strings...
local t = {['Test']}
table.remove(t, 'Test') >Error table.remove(t, 1) >Error
Make a function that will do the same job as table.remove() but so that it will also remove those that are indexed by a String...
"My Life is going Good... but..." |
|
|
| Report Abuse |
|
|
vacha
|
  |
| Joined: 06 Jan 2011 |
| Total Posts: 1993 |
|
|
| 20 May 2015 05:41 PM |
@warspy, How does table.destroy work?
while true do the do |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 05:42 PM |
"but so that it will also remove those that are indexed by a String..." lol why do just strings? many dictionaries have userdata and other stuff as their keys. |
|
|
| Report Abuse |
|
|
vacha
|
  |
| Joined: 06 Jan 2011 |
| Total Posts: 1993 |
|
|
| 20 May 2015 05:43 PM |
Sorry ignore my previous post. I thought it destroyed the table, instead it table.removes :D
while true do the do |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 05:47 PM |
@Kap
> local t = {"hi"} table.remove(t, 1) print(t[1]) nil
table.remove takes an index as the 2nd argument. If you wanna destroy all values equal to "hi" you can use table.destroy in my ExtendedTable module :) |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 05:48 PM |
| Oh he meant indexes when he said it don't work with strings (sorry that would be my fail lol) |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 05:50 PM |
| The idea behind table.remove is to shift all the values back when you nil the index, so please explain how would that work if you're using a dictionary instead of a numerical array :) |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 06:07 PM |
I've added 2 new functions
table.shuffle table.random
table.random returns a random value in the table (it's also dictionary compatible) |
|
|
| Report Abuse |
|
|
| |
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 20 May 2015 06:26 PM |
| Make table.shuffle dictionary compatible :) |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 06:29 PM |
@rayk Sounds... interesting.
This is the shuffle function (thanks Jarod)
table.shuffle = function(tab) local t = {} for i = 1,#tab do table.insert(t, table.remove(tab, math.random(#tab))) end table.clear(tab) for i,v in pairs(t) do tab[i] = v end return tab end
I don't really see me making that dictionary compatible in the future, plus it doesn't really make sense to shuffle a dictionary lol. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 20 May 2015 06:30 PM |
| Oh yeah, there's no point to shuffling a dictionary...lol im a noob |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 06:31 PM |
| Do you have any new function suggestions? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 20 May 2015 06:33 PM |
@warspyking Some are quite useful: map :: (a -> b) -> [a] -> [b] foldl :: (b -> a -> b) -> b -> [a] -> b (and foldr?) scanl :: (b -> a -> b) -> b -> [a] -> [b] (and scanr?) any/all :: (a -> Bool) -> [a] -> Bool iterate :: (a -> a) -> a -> [a] replicate :: Int -> a -> [a] take/drop :: Int -> [a] -> [a] takeWhile/dropWhile :: (a -> Bool) -> [a] -> [a] splitAt :: Int -> [a] -> ([a], [a]) isPrefixOf/isSuffixOf/isInfixOf :: (Eq a) => [a] -> [a] -> Bool filter :: (a -> Bool) -> [a] -> [a] elem :: (Eq a) => a -> [a] -> Bool find :: (a -> Bool) -> [a] -> Maybe a findIndex :: (a -> Bool) -> [a] -> Maybe Int zip :: [a] -> [b] -> [(a, b)] zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] lines/words :: String -> [String]
|
|
|
| Report Abuse |
|
|
|
| 20 May 2015 06:35 PM |
@Dr01d
I'm sorry, I'm ridiculously dumb, could you give me example calls of those functions and what they would return using Lua syntax? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 20 May 2015 06:52 PM |
map :: (a -> b) -> [a] -> [b] map(func, list) -> list Applies a function to each element in a list, returns a list of the results map(f, {x0, x1, x2}) = {f(x0), f(x1), f(x2)}
foldl :: (b -> a -> b) -> b -> [a] -> b (and foldr?) fold(func, acc, list) -> result Folds a function over a list with an initial accumulator. e.g: foldLeft(f, a, {x0, x1, x2}) = f(f(f(a, x0), x1), x2) foldRight(f, a, {x0, x1, x2}) = f(x0, f(x1, f(x2, a)))
scanl :: (b -> a -> b) -> b -> [a] -> [b] (and scanr?) scan(func, acc, list) -> list of results Same as fold, but whereas fold returns the final result, scan returns a list of the intermediate results scanLeft(f, a, {x0, x1, x2}) = {f(a, x0), f(f(a, x0), x1), f(f(f(a, x0), x1), x2)
any/all :: (a -> Bool) -> [a] -> Bool any/all(predicate, list) -> bool Applies the predicate function (unary function that returns boolean) to each element in the list Any returns true if any of the results are true All returns true if all of the results are true
iterate :: (a -> a) -> a -> [a] iterate(func, element) -> list Repeatedly applies the function to an element, makes a list of the return values Haskell can represent infinite lists so it's fine there, in Lua would have to have a max length iterate(f, x) = {f(x), f(f(x)), f(f(f(x))), ...}
replicate :: Int -> a -> [a] replicate(length, element) -> list Builds a list of a certain length, all elements are the same replicate(5, x) = {x, x, x, x, x}
take/drop :: Int -> [a] -> [a] take/drop(length, list) -> list Take returns the first length elements of a list Drop returns the elements after the first length take(3, {a, b, c, d, e}) = {a, b, c} drop(3, {a, b, c, d, e}) = {d, e}
takeWhile/dropWhile :: (a -> Bool) -> [a] -> [a] takeWhile/dropWhile(predicate, list) -> list Same as above, but whilst elements match a predicate function, rather than set length takeWhile(function (x) return x < 3; end. {1, 2, 3, 4, 5}) = {1, 2} dropWhile(function (x) return x < 3; end. {1, 2, 3, 4, 5}) = {3, 4, 5}
splitAt :: Int -> [a] -> ([a], [a]) splitAt(index, list) -> (list, list) Splits a list around an index splitAt(3, {a, b, c, d, e}) = ({a, b, c}, {d, e})
isPrefixOf/isSuffixOf/isInfixOf :: (Eq a) => [a] -> [a] -> Bool isPrefixOf/isSuffixOf/isInfixOf(affix, list) -> bool Returns true if affix is prefix/suffix/infix of list
filter :: (a -> Bool) -> [a] -> [a] filter(predicate, list) -> list Applies the predicate to each element in the list, if it returns true then it gets put in the return list, else skipped over E.g. to find all baseparts in a model: filter(function (obj) return obj:IsA("BasePart"); end, model:GetChildren()) And combined with map: map(function (part) part.Transparency = 1; end, filter(function (obj) return obj:IsA("BasePart"); end, workspace:GetChildren()));
elem :: (Eq a) => a -> [a] -> Bool elem(element, list) -> bool Returns true if the element is in the list
find :: (a -> Bool) -> [a] -> Maybe a find(predicate, list) -> element or nil Finds the first element in a list for which the predicate is true. Returns nil if not found
findIndex :: (a -> Bool) -> [a] -> Maybe Int findIndex(predicate, list) -> index or nil Same as above but returns the index of the element
zip :: [a] -> [b] -> [(a, b)] zip(left, right) -> list of pairs Takes 2 lists and zips them together zip({a, b, c}, {x, y, z}) = {{a, x}, {b, y}, {c, z}}
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith(func, left, right) -> list Same as above but you can specify how to combine the elements E.g. to find the dot product of 2 vectors (expressed as lists) local a = {x0, y0, z0}; local b = {x1, y1, z1}; -- Get a list of products {x0 * x1, y0 * y1, z0 * z1} local products = zipWith(function (l, r) return l * r; end, a, b); -- Sum the products to get dot product x0*x1 + y0*y1 + z0*z1 local dotProduct = foldLeft(function (l, r) return l + r; end, products); |
|
|
| Report Abuse |
|
|
|
| 20 May 2015 07:13 PM |
I'm still confused, what are
fold scan prefix/suffix/infix zipwith
and what practical uses do they have?
BTW I've already implemented elem, find and findindex in the 1 function (table.find) |
|
|
| Report Abuse |
|
|