Convenient utilities not included with the standard Lua install

Hello guys,

https://github.com/stephannvasconcelos/lua.util

I started to develop some utilities to Lua, I started with Array. Pull requests are welcome. There are several features to be made, they will be implemented gradually. I hope it’s useful for you as it is being for me. Issues can be opened to report bugs or feature requests.

A real example:

Creating an array of enemies:

enemies = Array:new()

Adding enemies to array:

enemies:add( some\_enemy ) enemies:add( other\_enemy )

Traversing enemies array to update each enemy:

enemies:each\_do(function( enemy ) enemy:update() end)

Deleting enemies that are dead:

enemies:delete\_if(function( enemy ) return enemy:is\_dead() end)

HI:

I think your Array module is great is a great, I have found my self in the need of an array a lot of time when coding in Corona.

Just a couple of method that would be great to expand your current Array:

-removeAt(index) Removes the element at the specified position.

I think this function could be really helpful, right now the only way for deleting an element you have is using delete_if(func) but sometimes you just want to delete a specific element by its index. With the current methods you have is actually very simple to achieve this, something like setting the element at index equal to null and a call to compact should do the trick.

-**getCount() **Returns the number of elements in the array

I know this is a very simple one, but really useful and its good to have so enclosure, and avoid direct access to the array holding the data to get number of elements.

-**insert(element, position) **Insert the specified element at the specified position.

-Remove(element) Remove the first occurrence o a specific element

Sometimes is more easy to remove an element you already have than storing the index somewhere to later call a removeAt(…) and keeping in mind that the actual index of the element can change.

Well I can continue adding really useful methods to this list like indexOf(element)addRange(array)insertRange(array, index), clear(), but to start I think those are good.

Once again nice job with that array module, and let me know if you want any help expanding it, I will very glad to help, I already read your entire code and is very easy to follow.

Regards

Hello Mad Screen, thanks for the great feedback.

I’m gradually evolving the project, the delete_at was done in the last commit.

array = Array:new() array:add('banana') array:add('apple') array:add('grape') array:add('orange') removed\_element = array:delete\_at(2) print(removed\_element) --prints 'apple' print(array:size()) --prints '3' print(array:at(2)) --prints 'grape'

The other features you mentioned are planned to be implemented. The project is open source,contributions are welcome. I’ll be glad with your help.

To run tests, on terminal use:

lua array\_test.lua

Do I define you as a contributor or will you send pull requests?

Thanks again.

A real example:

Creating an array of enemies:

enemies = Array:new()

Adding enemies to array:

enemies:add( some\_enemy ) enemies:add( other\_enemy )

Traversing enemies array to update each enemy:

enemies:each\_do(function( enemy ) enemy:update() end)

Deleting enemies that are dead:

enemies:delete\_if(function( enemy ) return enemy:is\_dead() end)

HI:

I think your Array module is great is a great, I have found my self in the need of an array a lot of time when coding in Corona.

Just a couple of method that would be great to expand your current Array:

-removeAt(index) Removes the element at the specified position.

I think this function could be really helpful, right now the only way for deleting an element you have is using delete_if(func) but sometimes you just want to delete a specific element by its index. With the current methods you have is actually very simple to achieve this, something like setting the element at index equal to null and a call to compact should do the trick.

-**getCount() **Returns the number of elements in the array

I know this is a very simple one, but really useful and its good to have so enclosure, and avoid direct access to the array holding the data to get number of elements.

-**insert(element, position) **Insert the specified element at the specified position.

-Remove(element) Remove the first occurrence o a specific element

Sometimes is more easy to remove an element you already have than storing the index somewhere to later call a removeAt(…) and keeping in mind that the actual index of the element can change.

Well I can continue adding really useful methods to this list like indexOf(element)addRange(array)insertRange(array, index), clear(), but to start I think those are good.

Once again nice job with that array module, and let me know if you want any help expanding it, I will very glad to help, I already read your entire code and is very easy to follow.

Regards

Hello Mad Screen, thanks for the great feedback.

I’m gradually evolving the project, the delete_at was done in the last commit.

array = Array:new() array:add('banana') array:add('apple') array:add('grape') array:add('orange') removed\_element = array:delete\_at(2) print(removed\_element) --prints 'apple' print(array:size()) --prints '3' print(array:at(2)) --prints 'grape'

The other features you mentioned are planned to be implemented. The project is open source,contributions are welcome. I’ll be glad with your help.

To run tests, on terminal use:

lua array\_test.lua

Do I define you as a contributor or will you send pull requests?

Thanks again.

Hi. is this new Array fastest than using table? How fastest is it?

Thanks

Hi gyus,

When I switch (using composer) from scene1 to scene2, my display objects inserted into scene1 are visible in scene2 … ??

What i’m doing wrong? I’m using scene:insert(…) to add my display objects

Thanks

Instead scene:insert(), use scene.view:insert()
More information on: https://docs.coronalabs.com/guide/system/composer/index.html

Hi. is this new Array fastest than using table? How fastest is it?

Thanks

Hi gyus,

When I switch (using composer) from scene1 to scene2, my display objects inserted into scene1 are visible in scene2 … ??

What i’m doing wrong? I’m using scene:insert(…) to add my display objects

Thanks

Instead scene:insert(), use scene.view:insert()
More information on: https://docs.coronalabs.com/guide/system/composer/index.html