Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arithemetic on multiple return values lua

Tags:

lua

Is it possible to perform arithmetic on multiple values in Lua. I am using Lua for windows 5.1.4.

Currently I have to put the multiple values into a table and then unpack them, and I would like to be able to skip that step.

Is it possible.

Here is what I currently have:

function numsToStr(...)
    local nums = {}
    for i,v in ipairs({...}) do
         nums[i] = v + string.byte('A') - 1
    end
    return string.char(unpack(nums))
end

What I want is to be able to do this

function numsToStr(...)
   return string.char(...+string.byte('A')-1)
end
like image 998
Taka Avatar asked Feb 15 '23 16:02

Taka


2 Answers

No, it is not possible to do arithmetic on multiple values in Lua.

like image 65
lhf Avatar answered Feb 23 '23 18:02

lhf


It's not possible to do "directly", but you can implement "map" function, similar to what you've done. Some relevant resources: Short anonymous functions, thread on Perl-like map/grep functions, and map and other functions. Also take a look at list comprehensions in Penlight.

like image 29
Paul Kulchenko Avatar answered Feb 23 '23 20:02

Paul Kulchenko