# DefTable - Lua Table Tools

**URL:** <https://forum.defold.com/t/deftable-lua-table-tools/17578>\
**Category:** The Defoldmine\
**Created:** [March 8, 2018, 12:51am UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578 "2018-03-08T00:51:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rantingbob](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/rantingbob/32/3636_2.png) [@rantingbob](https://forum.defold.com/u/rantingbob)\
**Post date:** [March 8, 2018, 12:51am UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/1 "2018-03-08T00:51:43Z")

</div>

I got fed up of writing basic table functions over and over again, so I packaged them up and put them on the Asset Portal at:  
[https://www.defold.com/community/projects/101547/](https://www.defold.com/community/projects/101547/)

It is licensed under the [UnLicense](https://unlicense.org/), a.k.a. it’s public domain and you can do whatever you like.

It’s also on GitHub at [https://github.com/ShriekBob/DefTable](https://github.com/ShriekBob/DefTable)

It has the following functions:

### List Like Tables

- index\_of
- for\_each
- for\_each\_with\_index
- map
- fold\_left
- fold\_right
- filter
- partition
- zip
- stitch
- all
- any
- member
- max
- min
- sum
- product

## Hash Like Tables

- key\_of
- index\_of
- for\_each
- map
- reduce
- filter
- partition
- all
- any
- is\_key
- is\_value
- keys
- values

If you spot any awful bugs, or have any issues (or if I’m not following some best practice) just let me know 🙂

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 8, 2018, 6:29am UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/2 "2018-03-08T06:29:54Z")

</div>

Cool! Some questions and thoughts:

- Why the `local M = M or {}`?
- Why not only have `for_each`? What’s the need for `for_each_with_index`? Can’t `for_each` always pass the index and then it’s up to the user to use or ignore the returned index.
- I’d like to see a short comment on each function that describes what it does and it’s input and output. Many of them are obvious just through their names but others need explanation I think.

---

<div class="post-metadata">

**Author:** ![rantingbob](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/rantingbob/32/3636_2.png) [@rantingbob](https://forum.defold.com/u/rantingbob)\
**Post date:** [March 8, 2018, 8:00am UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/3 "2018-03-08T08:00:26Z")

</div>

1. I…don’t remember. I’ve been doing it for years, and I remember reading somewhere you should do it, but I can’t find it now…so maybe I made it up? I guess I’ll stop doing it, and change it.

2. I preferred to be explicit, if it’s more idiomatic to do that I’ll happily change it.

3. Can you give me some examples of which were confusing? Happy to comment, I’d just like to understand a bit better.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 8, 2018, 8:22am UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/4 "2018-03-08T08:22:05Z")

</div>

> [@rantingbob](#):
>
> I remember reading somewhere you should do it, but I can’t find it now…so maybe I made it up? I guess I’ll stop doing it, and change it.

Well, the check would use a global M if that exists, otherwise create a new table to hold the module contents. Not sure why you’d want a global M though…

> [@rantingbob](#):
>
> I preferred to be explicit, if it’s more idiomatic to do that I’ll happily change it.

I have no string opinion about it, but to me it would have made more sense to always pass the index in for\_each and then it’s up to the user to decide if it’s used or not.

> [@rantingbob](#):
>
> Can you give me some examples of which were confusing? Happy to comment, I’d just like to understand a bit better.

Well, in general I prefer to have a readme (or generated docs from LDoc) to look at rather than having to go to the code to figure out that `hash_util.reduce` takes it’s arguments in the `func, accumulator, hash` order. I was also uncertain of what `zip`, `stitch` and `partition` did.

---

<div class="post-metadata">

**Author:** ![rantingbob](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/rantingbob/32/3636_2.png) [@rantingbob](https://forum.defold.com/u/rantingbob)\
**Post date:** [March 8, 2018, 10:06am UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/5 "2018-03-08T10:06:29Z")

</div>

Okay cool, I’ll make some changes probably friday night and update it.

---

<div class="post-metadata">

**Author:** ![rantingbob](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/rantingbob/32/3636_2.png) [@rantingbob](https://forum.defold.com/u/rantingbob)\
**Post date:** [March 8, 2018, 8:03pm UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/6 "2018-03-08T20:03:02Z")

</div>

Done.

I removed the _M = M or {}_ so it’s now just _M = {}_.

I added much more complete docs to the README.

I also renamed _for\_each\_with\_index_ to _for\_each_ and removed the previous implementation of _for\_each_ as you suggested.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 8, 2018, 10:02pm UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/7 "2018-03-08T22:02:39Z")

</div>

Nice! Excellent improvements! The readme will make the modules a lot more useful.

---

<div class="post-metadata">

**Author:** ![rantingbob](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/rantingbob/32/3636_2.png) [@rantingbob](https://forum.defold.com/u/rantingbob)\
**Post date:** [March 8, 2018, 10:50pm UTC](https://forum.defold.com/t/deftable-lua-table-tools/17578/8 "2018-03-08T22:50:18Z")

</div>

I just hope it’s useful 😃
