minmax

function minmax( x, ... ) --> min, max

Description

Returns the parameter with the minimum and maxium value, according to the Lua operator <.

Parameters

x

Basic value for the comparsion.

Other values for the comparsion.

Return Values

min

The minimum value.

max

The maximum value.

Code

--ZFUNC-minmax-v1
local function minmax( x, ... ) --> min, max
   local min = x
   local max = x
   for _, v in ipairs{ ... } do
      if v < min then min = v end
      if v > max then max = v end
   end
   return min, max
end

return minmax

Examples

local t = require( "taptest" )
local minmax = require( "minmax" )

min, max = minmax( 1, 2, 3, 4, 5 )
t( min, 1 )
t( max, 5 )

min, max = minmax( 8, 1 )
t( min, 1 )
t( max, 8 )

min, max = minmax( 7 )
t( min, 7 )
t( max, 7 )

t()

Inspired By