[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: select
- From: Rici Lake <lua@...>
- Date: Thu, 19 Aug 2004 19:07:12 -0500
Sorry, I guess I should have been clearer about that. This was a thread 
from some months ago.
A tuple is (for me in this context) an immutable list of objects. 
Functional tuples are a representation of tuples as functions; the 
function returns the tuple'd objects as a multiple return.
A simple pure-lua implementation: (leaving out the "select" feature)
> function tuple(...) return function() return unpack(arg) end end
> foo = tuple(2, 3, 4)
> =foo()
2       3       4
But one can do it *much* more efficiently in C: the following tiny patch
is a very simple implementation, but arguably lacks sufficient 
error-checking.
Implementing tail (or select, but I think tail is better) means actually
writing the tuple function instead of using lua_pushupvalues but it's 
only
a couple more lines of code.
The end result is a tuple represented as a simple lua_CFunction with a 
vector
of objects (but limited in size because nupvalues is a byte).
Some less simple implementations are in the mailing list archives.
R.
-------PATCH-----------
diff -r -u lua-5.0.2/src/lib/lbaselib.c 
lua-5.0.2-simpletuple/src/lib/lbaselib.c
--- lua-5.0.2/src/lib/lbaselib.c	2004-03-03 19:45:13.000000000 -0500
+++ lua-5.0.2-simpletuple/src/lib/lbaselib.c	2004-05-10 
10:34:32.000000000 -0500
@@ -504,6 +504,11 @@
 /* }====================================================== */
+static int l_tuple (lua_State *L) {
+  lua_pushcclosure(L, lua_pushupvalues, lua_gettop(L));
+  return 1;
+}
+
 static const luaL_reg base_funcs[] = {
   {"error", luaB_error},
@@ -531,6 +536,7 @@
   {"dofile", luaB_dofile},
   {"loadstring", luaB_loadstring},
   {"require", luaB_require},
+  {"tuple", l_tuple},
   {NULL, NULL}
 };