DefTable - Lua Table Tools

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/

It is licensed under the UnLicense, 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

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 :slight_smile:

10 Likes

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.
3 Likes
  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.

1 Like

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…

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.

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.

1 Like

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

3 Likes

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.

3 Likes

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

1 Like

I just hope it’s useful :smiley:

1 Like