lua-users home
lua-l archive

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


> lua_insert(L,n) is a rotation: it takes the top element and puts it at 
position
>cn, pushing everything in between

> lua_pushvalue(L,n)+lua_remove(L,n) does a rotation in the other 
direction.

> Do you really miss more general rotations?

Not often... but when I do, it is really annoying not having it.

For example, consider the implementation of curry:

int l_curry(lua_State *L) {
  int top = lua_gettop(L);
  int nups = lua_pushupvalues(L);
  /* Now, what I want to do is rotate the stack by top. */
  /* I can do it with gettop(L)-top lua_insert()'s, but */
  /* that is O(top*nups) and it should be O(top+nups).  */
  /* Even though top and nups are likely to  be small,  */
  /* that seems annoying. So what I do is: */
  int i = 1;
  for (; i <= top; ++i) lua_pushvalue(L, i);
  lua_call(L, top + nups - 1, LUA_MULTRET);
  return lua_top(L) - top;
  /* But that seems wasteful of stack space */
}

I think this would be cleaner and faster if I could say:

int l_curry(lua_State *L) {
  int top = lua_gettop(L);
  int nups = lua_pushupvalues(L);
  lua_rotate(L, top + nups, -top);
  lua_call(L, top + nups - 1, LUA_MULTRET);
  return lua_top(L);
}

I could probably dig out some other examples, but that one was handy.

R.