This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kouksi44's profile picture

Generic Table... kind of

Started by Kouksi44, 19 January 2017 - 10:16 PM
Kouksi44 #1
Posted 19 January 2017 - 11:16 PM
This is probably pretty useless, but hey maybe someone can make something out of it…

This neat little tool allows you to define Lists that hold values of a certain type only. Doesn't sound like much but atleast it looks kind of pretty…


local myList = List : number {
  34;
  35;
  36;
}

As you can see you declare a List by writing:

List : <YourSuperSpecialType> {}

You can pass some initial values to the List by passing a table after the List type.

Another super nice feature is, that you can create your own "types" for your Lists. Lets say you want a List of e.g. Vectors.
That can be done by declaring a table with a __type field. Now if that field is defined to be
__type = "Vector"
these tables will be treated as if they were of type "Vector".
Comes in handy if you're writing some oop code and want to have List of your objects.

Install:
pastebin get 9mJL4rgH

Let me know if this usefull in any way or not ^^
H4X0RZ #2
Posted 21 January 2017 - 01:02 AM
Actually, this is really awesome! I like the syntax a lot, and it feels much better to work with more restricted tables than this "I take everything I can"-type of "table" we have in Lua. I'm trying to get into function programming and this might come in handy when doing some "functional lua" ;)/>
Kouksi44 #3
Posted 23 January 2017 - 12:30 AM
Somehow I was able to forget to explain pretty much anything except for the most basic features… great.
I added a few more helper functions, most of them are simply their table.XXX() equivalent wrapped in another function to make them work with Lists.

Things you can now use:
Spoiler

<yourList>:length() -- returns the length of your list
<yourList>:getType() -- returns the type of the List as a string
<yourList>:remove(Key) -- removes the given key from the List
<yourList>:sort(ComparisonFunc) -- sorts the List using table.sort and passing the comparison function if one is passed as a parameter
<yourList>:concat(Separator,RangeStart,RangeStop) -- concatenates the List to a string

Additionally, if you just want to add a value to the end of the list , you can now use this syntax:

<yourList> = <yourList> + 77


Updated the pastebin link in the main post !

Edit: Just in case anyone was wondering, yes you can make Lists containing Lists. List objects do have a __type field defining them as List objects.
Edited on 22 January 2017 - 11:34 PM