Finding the minimum value... (not math.min)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Finding the minimum value... (not math.min)

Post by Robin »

bartbes's is better.
Help us help you: attach a .love.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

Re: Finding the minimum value... (not math.min)

Post by napco »

Or, again:

Code: Select all

function get_min(t)
    min = math.min(unpack(t))
    for i, v in ipairs(t) do
        if v == min then
            return v, i
        end
    end
end
I LOVE programming because it's almost impossible to have only one solution to a specific problem!
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Finding the minimum value... (not math.min)

Post by appleide »

Code: Select all

function min_index(first, ...)
	return math.min(first, ...) == first and 1 or min_index(...) + 1
end

function test(...)
	print(min_index(...))
end

test(1, 2, 3, 4) -- prints 1

test(4, 3, 1, 5, 6, 7) -- prints 3
Who needs for loops 8-)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests