Lua - Range Iterators



A range function is the function which when called returned the range of numbers as per the given condition. In Lua, we can define a range iterator easliy.

Example - Using Numeric For loop

Before writing a range iterator, let's us try few ranges using numeric for loop.

A sequence from 1 to 10 using numeric for loop.

main.lua

-- Print a sequence from 1 to 10, increment by 1
for i = 1, 10, 1 do
   print(i)
end

Output

When you build and execute the above program, it produces the following result −

1
2
3
4
5
6
7
8
9
10

Example - Print a sequence

Following example prints a sequence from 7 to -2 using numeric for loop.

main.lua

-- Print a sequence from 7 to -2, decrement by 1
for i = 7, -2, -1 do
   print(i)
end

Output

When you build and execute the above program, it produces the following result −

7
6
5
4
3
2
1
0
-1
-2

Example - Range iterator for Generic for loop

Now in order to achieve above functionality using generic for loop, we need to create a range iterator function.

Range Iterator function

range = function (from, to, step)
   -- if from is not specified, end the loop
   if from == nil then
      return end

   -- if to is not specified
   if not to then
      -- set to as from
      to = from
      -- set from as 0 or to if to > 0
      from  = to == 0 and 0 or (to > 0 and 1 or -1)
   end

   -- update step
   step = step or (from < to and 1 or -1)

   -- step back start
   from = from - step

   -- Closure function to return key value
   rangeIterator = function ()
      if from == to then
         return nil
      end
      from = from + step
      return from, from
   end

   return rangeIterator
end

Use cases of Range Iterator function

-- iterate 1 to 10, increment by 1
for i in range( 10) do 
   print(i) 
end

-- iterate -1 to -10, decrement by 1
for i in range(-10) do 
   print(i) 
end

-- iterate 7 to -2, decrement by 1
for i in range(7, -2) do 
   print(i) 
end

-- iterate 3 to 27, increment by 3
for i in range(3, 27, 3) do 
   print(i) 
end

Complete Examples of Range Iterator

In this example, we're using range Iterator for positive numbers sequence.

main.lua

range = function (from, to, step)
   -- if from is not specified, end the loop
   if from == nil then
      return end

   -- if to is not specified
   if not to then
      -- set to as from
      to = from
      -- set from as 0 or to if to > 0
      from  = to == 0 and 0 or (to > 0 and 1 or -1)
   end

   -- update step
   step = step or (from < to and 1 or -1)

   -- step back start
   from = from - step

   -- Closure function to return key value
   rangeIterator = function ()
      if from == to then
         return nil
      end
      from = from + step
      return from, from
   end

   return rangeIterator
end

-- iterate 1 to 10, increment by 1
for i in range( 10) do 
   print(i) 
end

Output

When you build and execute the above program, it produces the following result −

1
2
3
4
5
6
7
8
9
10

Example - Using range Iterator for negative numbers sequence

Following example shows the use of iterator to create a sequence of negative numbers.

main.lua

range = function (from, to, step)
   -- if from is not specified, end the loop
   if from == nil then
      return end

   -- if to is not specified
   if not to then
      -- set to as from
      to = from
      -- set from as 0 or to if to > 0
      from  = to == 0 and 0 or (to > 0 and 1 or -1)
   end

   -- update step
   step = step or (from < to and 1 or -1)

   -- step back start
   from = from - step

   -- Closure function to return key value
   rangeIterator = function ()
      if from == to then
         return nil
      end
      from = from + step
      return from, from
   end

   return rangeIterator
end

-- iterate 7 to -2, decrement by 1
for i in range(7, -2) do 
   print(i) 
end

Output

When you build and execute the above program, it produces the following result −

7
6
5
4
3
2
1
0
-1
-2
Advertisements