0 Replies - 1349 Views - Last Post: 02 June 2010 - 08:20 PM

#1 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

[LUA]Stateless Iterator

Posted 02 June 2010 - 08:20 PM

Description: Should work fine with Lua 5.1.3In general, a custom iterator is implemented with a closure. This is fine (and recommended!), however, in situations where maximum efficiency is needed, this overhead can slow you down. By using a loop to save state, you can write a more efficient iterator.
-- A trivial iterator that works without 
-- closure overhead. It saves its state
-- with the for loops calling it

-- The actual iterator function.
-- It only returns the next element if
-- it's larger then the previous element.
-- This isn't actually important...
local function _set(table, i)
	i = i + 1;
	local x = table[i];
	local y = table[i-1];

	if y == nil then
            return x;
        elseif x <= y then 
            return nil;
        else 
            return x;
        end
end

-- the actual function being used and to
-- be called. 
function set(t)
    -- this might confuse people, so here's
    -- a quick explanation. This function returns
    -- three things. The first is function the loop
    -- will call on each iteration, the second is the
    -- invariant state (the object we're working with),
    -- and the last is the control variable (which we 
    -- increase on each iteration).
    --
    -- Basically, upon each iteration, the loop will do this:
    --     _set(t,i); <-- where i is the value of the ctrl var.
 
    return _set, t, 0;
end


-- an example is helpful...
local t = {1,2,6,5}
for v in set(t) do
    print(v);
end


Is This A Good Question/Topic? 0
  • +

Replies To: [LUA]Stateless Iterator

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: [LUA]Stateless Iterator

Posted 02 June 2010 - 08:20 PM

Description: Should work fine with Lua 5.1.3In general, a custom iterator is implemented with a closure. This is fine (and recommended!), however, in situations where maximum efficiency is needed, this overhead can slow you down. By using a loop to save state, you can write a more efficient iterator.
-- A trivial iterator that works without 
-- closure overhead. It saves its state
-- with the for loops calling it

-- The actual iterator function.
-- It only returns the next element if
-- it's larger then the previous element.
-- This isn't actually important...what is
-- important to note is that unless i is 
-- returned with the value, the loop will
-- get confused.
local function _set(table, i)
	i = i + 1;
	local x = table[i];
        if x == nil then
            return nil;
        else
            local y = table[i-1];
            if y == nil then
                return i, x;
            else
                if x <= y then
                    return nil;
                else
                    return i, x;
                end
            end
       end
end

-- the actual function being used and to
-- be called. 
function set(t)
    -- this might confuse people, so here's
    -- a quick explanation. This function returns
    -- three things. The first is function the loop
    -- will call on each iteration, the second is the
    -- invariant state (the object we're working with),
    -- and the last is the control variable (which we 
    -- increase on each iteration).
    --
    -- Basically, upon each iteration, the loop will do this:
    --     _set(t,i); <-- where i is the value of the ctrl var.
 
    return _set, t, 0;
end


-- an example is helpful...
local a = {1,2,3,5,19,35,72,88,4}
for i, v in set(a) do
    print(v);
end

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1