How can I shorten this multiple assignment in Lua?


Is there any better way to golf this (other than removing newlines/indentation)?

  local c=s(a,b)
  local d=c
  for i,v in ipairs{...} do d=d:push(v) end
  return c

I'm particularly bothered by the double local usage, but as Lua does not have multiple assignment, and I cannot use function parameters to declare the locals, I can't see a way to remove them.

For those who care, this is the implementation of the __call metamethod in my quick hack of a linked-list ~class used in this answer.


Posted 2016-08-26T23:24:10.163

Reputation: 213

Lua does have multiple assignment:

– nolan – 2016-08-26T23:39:24.437

That's parallel, assignment, though. See this question. You cannot use it to share values.

– Phrogz – 2016-08-26T23:40:31.673

1Could you perhaps declare both variables in the same line and then do assignment later? Like local c,d=s({v=b},a),0 d=c? That takes out one of the local keywords. I don't know anything about Lua, though. – Value Ink – 2016-08-26T23:45:49.227

Yes that can be done. – nolan – 2016-08-26T23:49:23.523

@ValueInk Post that as an answer! Note that you don't need the ,0. – Phrogz – 2016-08-27T00:55:02.027

Does s have side effects? If not I guess you could do local c,d=s(a,b),s(a,b). – feersum – 2016-08-27T03:08:13.097

@feersum Good question. Yes, s() creates a table and returns it. The shared value is a reference to an element, not a simple value. – Phrogz – 2016-08-27T03:12:35.003



As requested in the comments, here it is as an answer.

local c,d=s(a,b)  -- d is nil

This saves you one local declaration, should give you -5 bytes.

Value Ink

Posted 2016-08-26T23:24:10.163

Reputation: 10 608