Thursday, May 3, 2012

Multiple Assignment 를 이용한 간결한 구현 예

 function getMinAndMax(a)  
      local min_i, max_i = -1, -1  
      local min, max = math.huge, -math.huge  
      for i, v in pairs(a) do  
           if (v < min) then min, min_i = v, i end  
           if (v > max) then max, max_i = v, i end  
      end  
      return min_i, max_i, min, max  
 end  
   
 local a = {10, 60, 5, 3, 4, 30, 40, 20}  
 local min_i, max_i, min, max = getMinAndMax(a)  
 print("min = a[" .. min_i .. "] = " .. min)  
 print("max = a[" .. max_i .. "] = " .. max)  
   

No comments:

Post a Comment