lua-users home
lua-l archive

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


On Tue, Dec 08, 1998 at 06:36:27PM -0200, Luiz Henrique de Figueiredo wrote:
> >From: David Jeske <jeske@home.chat.net>
> >
> >I propose the following idea for making a block of code which runs
> >inside the local-scope of the caller, but is otherwise similar to a
> >function and evaluates as such:
> >
> >macro for(from,to,code)
> >   local i = from
> >
> >   while (i<to) do
> >      code(i);
> >      i = i + 1;
> >   end
> >end
> >
> >
> >for(1,2, macro (x)
> >  print("iteration: ".. tostring(x));
> >end);
> 
> I'm probably missing something here, but how is this different from
> a function?  What you can write right now (and it works!) 

I'm sorry, in all my examples I never once used the feature I wanted
to have, because I didn't actually _do_ anything in any of the
loops. Lets try again:

function correlate_data(tbl1,tbl2)
  local output_tbl = {};

  for(1,2,macro(x)
     output_tbl[x] = tbl2[tbl1[x]];
  end);

  return (output_tbl);
end

If I use functions to do the 'for' and inner construct, then have
something like:


function for(start,finish,args,fn) 
   local i = start;
   local move_tbl = function(tbl)
     local temp_val;
     local o_temp_val;
     local iter = 1;
    
     temp_val = tbl[iter];
     while(temp_val) do
        o_temp_val = tbl[iter+1];
        tbl[iter+1] = temp_val;
        temp_val = o_temp_val;
        iter = iter + 1;
     end
   end

   move_tbl(args); -- move everything in the table down so there
                   -- is room for the iterator...

   while (i<finish) do
       args[1] = i;
       call(fn,args);
       i = i + 1;
   end
end

function correlate_data(tbl1,tbl2)
   local output_tbl = {};

   for(1,2, {tbl1,tbl2,output_tbl}, function (x,tbl1,tbl2,output_tbl)
      output_tbl[x] = tbl2[tbl1[x]];
   end);

   return output_tbl;
end;


That seems like alot more trouble to me... I'll have to think on it
and come up with some better examples.

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net