[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Think different
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 31 Aug 2015 09:15:10 +0200
2015-08-31 6:36 GMT+02:00 Daurnimator <quae@daurnimator.com>:
> I was playing around creating different things using this style, and
> discovered a real need for a 'settop' like function from lua.
> That is, a function that removes trailing vararg values.
It's in the not very popular [1] xtable [2] module.
~~~
xtable.tuple.keep(count,...)
   Returns `count` arguments, starting at the first extra argument. As in
   the case of select, a negative number indexes from the end (-1 is the
   last argument).
~~~
> static int settop(lua_State *L) {
>         int n = luaL_checkinteger(L, 1);
>         luaL_argcheck(L, n >= 0, 1, NULL);
>         luaL_checkstack(L, n+1, NULL);
>         lua_settop(L, n+1);
>         return n;
> }
The xtable implementation is:
/* tuple.keep(count,...) */
static int tuple_keep(lua_State *L) {
  int count=lua_absindex(L,luaL_checkint(L,1));
  lua_settop(L,count+1);
  return count;
}
The main differences are that my implementation does not
allow 'count' to be greater than select('#',...) and yours does
not respect a negative count. I.e., 'keep' emphasizes kinship
to 'select' and 'settop' emphasizes kinship to `lua_settop`.
> It wouldn't be a bad addition to the lua base library.
While we are at it, why not burn the candle at both ends?
extract(from,to,...)
   Returns arguments `from` to `to`, providing explicit nils when
   `...` is too short. As in the case of select, a negative number
   indexes from the end (-1 is the last argument).
[1] Last on the shortlist with 6 stars.
[2] https://github.com/dlaurie/xtable