Advertisement
rockbandcheeseman

Iterative Library

Jul 19th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. -- Iterative Functions
  2.  
  3. --[[
  4.     Documentation:
  5.    
  6.     These are all iterative functions.  Iterative functions are used in loops (pairs and ipairs are two examples) and can be very useful if you want to loop through a table or numbers in a specific order.
  7.    
  8.     All of the following examples will be assuming the following table exists:
  9.    
  10.     t = {}
  11.     t[1] = 7
  12.     t[2] = 8
  13.     t[5] = -2
  14.     t["cheese"] = 10
  15.     t["derp"] = -14
  16.    
  17.     Here are descriptions of each function and how to use them:
  18.    
  19.     opairs
  20.    
  21.         Syntax:
  22.        
  23.             for k,v in opairs(t) do
  24.                 hprintf(k .. ": " .. v)
  25.             end
  26.        
  27.         When to use:
  28.        
  29.             -- The function opairs is short for "ordered pairs".  This iterative function will loop through all elements of a table (including string keys) in alphanumeric order.
  30.             -- Contrary to most beginning scripters' beliefs, ipairs does NOT do this.  ipairs loops through a table beginning at index 1 and adds 1 to the index until the value at the current index is nil.
  31.             -- Example of why this is useful:
  32.            
  33.                 for k,v in opairs(t) do
  34.                     hprintf(k .. ": " .. v)
  35.                 end
  36.                
  37.                 >> 1: 7
  38.                 >> 2: 8
  39.                 >> 5: -2
  40.                 >> cheese: 10
  41.                 >> derp: -14
  42.                
  43.                 for k,v in ipairs(t) do
  44.                     hprintf(k .. ": " .. v)
  45.                 end
  46.                
  47.                 >> 1: 7
  48.                 >> 2: 8
  49.                
  50.                 -- ipairs only prints up to key 2 because t[3] = nil.  All other keys are disregarded by ipairs.
  51.                
  52.     rpairs
  53.    
  54.         -- See opairs for details; rpairs is exactly the same, but loops in reverse order.
  55.        
  56.     expairs
  57.    
  58.         Syntax:
  59.        
  60.             for k,v in expairs(t, function(key, value) return true end)
  61.                 hprintf(k .. ": " .. v)
  62.             end
  63.        
  64.         When to use:
  65.        
  66.             -- expairs allows you to loop through a table while using a function to determine which keys and values the loop should consider.
  67.             -- For example:
  68.            
  69.                 -- Only loop through numerical keys
  70.                 for k,v in expairs(t, function(key, value) return type(key) == "number" end)
  71.                     hprintf(k .. ": " .. v)
  72.                 end
  73.                
  74.                 >> 1: 7
  75.                 >> 2: 8
  76.                 >> 5: -2
  77.                
  78.     irand
  79.    
  80.         Syntax:
  81.        
  82.             for i in irand(min, max) do
  83.                 hprintf(i)
  84.             end
  85.            
  86.         When to use:
  87.        
  88.             -- irand iterates from min to max in a random order.
  89.             -- Example:
  90.            
  91.                 for i in irand(0,15) do
  92.                     say(getname(i))
  93.                 end
  94.                
  95.                 >> Oxide
  96.                 >> Nuggets
  97.                 >> Wizard
  98.                 >> Chalonic
  99.                
  100.    
  101.     Iterative functions can be tricky, so let me know if you need help with any of them.  PM me (Nuggets) at phasor.proboards.com for any questions you have.
  102. --]]
  103.  
  104. function opairs(t)
  105.    
  106.     local keys = {}
  107.     for k,v in pairs(t) do
  108.         table.insert(keys, k)
  109.     end
  110.     table.sort(keys,
  111.     function(a,b)
  112.         if type(a) == "number" and type(b) == "number" then
  113.             return a < b
  114.         end
  115.         an = string.lower(tostring(a))
  116.         bn = string.lower(tostring(b))
  117.         if an ~= bn then
  118.             return an < bn
  119.         else
  120.             return tostring(a) < tostring(b)
  121.         end
  122.     end)
  123.     local count = 1
  124.     return function()
  125.         if table.unpack(keys) then
  126.             local key = keys[count]
  127.             local value = t[key]
  128.             count = count + 1
  129.             return key,value
  130.         end
  131.     end
  132. end
  133.  
  134. function rpairs(t)
  135.  
  136.     local keys = {}
  137.     for k,v in pairs(t) do
  138.         table.insert(keys, k)
  139.     end
  140.     table.sort(keys,
  141.     function(a,b)
  142.         if type(a) == "number" and type(b) == "number" then
  143.             return a > b
  144.         end
  145.         an = string.lower(tostring(a))
  146.         bn = string.lower(tostring(b))
  147.         if an ~= bn then
  148.             return an > bn
  149.         else
  150.             return tostring(a) > tostring(b)
  151.         end
  152.     end)
  153.     local count = 1
  154.     return function()
  155.         if table.unpack(keys) then
  156.             local key = keys[count]
  157.             local value = t[key]
  158.             count = count + 1
  159.             return key,value
  160.         end
  161.     end
  162. end
  163.  
  164. function expairs(t, fn)
  165.  
  166.     local keys = {}
  167.     for k,v in opairs(t) do
  168.         if fn(k,v) then
  169.             table.insert(keys, k)
  170.         end
  171.     end
  172.     local count = 1
  173.     return function()
  174.         if table.unpack(keys) then
  175.             local key = keys[count]
  176.             local value = t[key]
  177.             count = count + 1
  178.             return key,value
  179.         end
  180.     end
  181. end
  182.  
  183. function irand(min, max)
  184.  
  185.     local u = {}
  186.     for i = min,max do
  187.         table.insert(u, i)
  188.     end
  189.     return function()
  190.         if table.unpack(u) then
  191.             local rand = getrandomnumber(1, #u + 1)
  192.             local value = u[rand]
  193.             table.remove(u, rand)
  194.             return value
  195.         end
  196.     end
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement