lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2014-04-10 10:15 GMT+02:00 steve donovan <steve.j.donovan@gmail.com>:
> On Thu, Apr 10, 2014 at 10:04 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> I actually have a whole library of `tuple` methods, which I find
>> I almost never use, but the name `unpackt` never occurred to me.
>
> As soon as I typed it, I realized it was ugly. How about vindex?

I called it "map". Most of the package mimics part of the Lua API, not
nearly as efficiently of course, but useful to develop the logic of one's
code in Lua before porting it to C.

John Hind collaborated with me on this project.

Here is the demo. Just for fun, I tried it with 5.3.0-work2 without
recompiling tuple.so, and it worked out of the box.

$ lua53 -l tuple test-tuple.lua
Welcome to the tuple library! The following functions are available:
fill  lua  map  merge  sort
                                       Lua code                        Result
----------------------------------------------- -----------------------------
  1.            lua.absindex(-2,11,22,33,44,55)                             4
  2.                lua.arith(0,11,22,33,44,55)                   11 22 33 99
  3.                lua.arith(1,11,22,33,44,55)                  11 22 33 -11
  4.                lua.arith(2,11,22,33,44,55)                 11 22 33 2420
  5.                lua.arith(3,11,22,33,44,55)                   11 22 33 44
  6.                lua.arith(4,11,22,33,44,55)                    11 22 33 0
  7.                lua.arith(5,11,22,33,44,55)                  11 22 33 0.8
  8.                lua.arith(6,11,22,33,44,55)                    11 22 33 0
  9.        lua.compare(-1,-3,0,11,33,55,22,44)                         false
 10.        lua.compare(-1,-3,1,11,33,55,22,44)                          true
 11.        lua.compare(-1,-3,2,11,33,55,22,44)                          true
 12.                   lua.concat({1},glue,{2})              table: 0x8282cc0
 13.               lua.copy(2,4,11,22,33,44,55)                11 22 33 22 55
 14.               lua.insert(2,11,22,33,44,55)                11 55 22 33 44
 15.                  lua.pop(2,11,22,33,44,55)                      11 22 33
 16.              lua.promote(1,11,22,33,44,55)                 22 33 44 55 1
 17.                 lua.push(2,11,22,33,44,55)             11 22 33 44 55 11
 18.               lua.remove(2,11,22,33,44,55)                   11 33 44 55
 19.              lua.replace(2,11,22,33,44,55)                   11 55 33 44
 20.               lua.settop(2,11,22,33,44,55)                         11 22
 21.                               lua.where(2)           test-tuple.lua:17:
 22.                               lua.where(3)           test-tuple.lua:68:
----------------------------------------------- -----------------------------
 23.                 map(math.sqrt,1,4,9,16,25)           1.0 2.0 3.0 4.0 5.0
 24.                map(math,"pi","huge","NaN")       3.1415926535898 inf nil
 25.                             fill(10,1,2,3)           1 2 3 1 2 3 1 2 3 1
 26.                            fill(-10,1,2,3)           3 1 2 3 1 2 3 1 2 3
 27.      sort(nil,5,4,6,7,5,6,9,8,1,4,2,8,5,7)   1 2 4 4 5 5 5 6 6 7 7 8 8 9
 28.            merge(up,1,5,8,1,3,5,7,2,4,6,8)               1 2 3 4 5 6 7 8

"glue" is an empty list with a __concat metamethod that strings lists
together. "up" is just an ordering function such as accepted by table.sort.