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( "tapered" )
local minmax = require( "minmax" )

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

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

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

t.done()

Inspired By