  lua-l archive

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

• Subject: Re: Determining which functions to call
• From: Björn De Meyer <bjorn.demeyer@...>
• Date: Mon, 20 Jan 2003 20:59:37 +0100

```Florian Berger wrote:
>
> Hi.
>
> I would like to make a script system which determines the order of needed
> function calls to solve a problem. The basic idea is that I know values of
> some variables and then I have functions which can or cannot solve the
> problem with existing variables.
>
> Simple example:
> I have a Circle-object which has two attributes: radius and diameter.
> I know the circle's radius but not the diameter.
> I have two functions: one which calculates diameter from radius and other
> which calculates area of circle using diameter.
> I would like to make my system solve the area of circle so, that it
> automatically solves the diameter for the area-function.
>
> Are there some examples of this kind of problems for LUA and do you think
> LUA is good choice for this kind of programming?
>
> Florian

This would be quite simple:
(Lua 5 beta code, I think it's PI in stead of math.pi in Lua 4)

function areafromdiameter(d)
return math.pi * d * d / 4
end

function calccircle(circle)
if circle.radius then --will be false if not defined.
circle.diameter = circle.radius * 2
end
return areafromdiameter(circle.diameter)
--This skips certain sanity checks that might be useful
end

print(calccircle({radius=4}))
print(calccircle({diameter=8}))

--Outputs:
--50.265482457437
--50.265482457437

--
"No one knows true heroes, for they speak not of their greatness." --
Daniel Remar.
Björn De Meyer
bjorn.demeyer@pandora.be

```