monome / norns

norns is many sound instruments.
http://monome.org
GNU General Public License v3.0
633 stars 147 forks source link

Add default "custom value" field in `tab.gather` #1571

Closed zjb-s closed 2 years ago

zjb-s commented 2 years ago

This allows copying a table by calling tab.gather(myTab). Previously you needed to call it as tab.gather(myTab,{})

ngwese commented 2 years ago

given that the stated purpose of the function is to combine two tables it seems odd to me to change it to make the second parameter optional.

it seems more appropriate for that caller of the function to handle the possibility of a passing a nil value by doing tab.gather(defaults, custom or {}). for some usage of the gather function it may be more appropriate to fail if the second argument is nil because it represents an error in the script itself.

zjb-s commented 2 years ago

i tend to use this function to return a plain copy of a table (not trivial in lua, unless i'm missing something big) rather than combine tables, but maybe that's more of an edge-case than I thought?

if that's the case, we can just close this. it's a minor point anyway. and your point about errors makes sense.

thank you greg!

ngwese commented 2 years ago

if you haven’t seen it i might suggest reading through this page http://lua-users.org/wiki/CopyTable, in particular it speaks to the ambiguity around the notion of copying.

this basic clone function at the top of the page would give you a more performant way to achieve the same behavior as calling table.gather with an empty table.

function table.clone(org)
  return {table.unpack(org)}
end
zjb-s commented 2 years ago

that's really helpful, thank you!!