lua-users home
lua-l archive

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


It was thus said that the Great Coroutines once stated:
> On Wed, Apr 9, 2014 at 11:20 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> > Though possible, the distinction between returning nothing and
> > returning some nils is messy --  select('#',fct(...)) -- and not
> > accepted practice.
> 
> I recognize this :>
> 
> My reasoning is that:
> 
> return
> return nil, 'error'
> 
> -- both of these suit propagating an error, but I feel like the 2nd
> should be used if you signal an error by explicitly returning nil.
> Unless you have an string-error to return, I feel like nothing would
> be more appropriate.
> 
> Not dropping this, I would really love to see a global max()/min() for
> metamethod __gt/__lt :p

function max(a,...)
  if not a then return nil end

  for i = 1 , select('#',...) do
    local b = select(i,...)
    if a < b then
      a = b
    end
  end
  return a
end

function min(a,...)
  if not a then return nil end
  
  for i = 1 , select('#',...) do
    local b = select(i,...)
    if a > b then
      a = b
    end
  end
  return a 
end

print(max("alpha","beta","gamma","delta"))
print(max())
print(max(1,2,3,4))
  
print(min("alpha","beta","gamma","delta"))
print(min())
print(min(1,2,3,4))
  
print(max("one",nil,"two"))

  -spc (It's a starting point ... )