lua-users home
lua-l archive

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


> It can be used to locate sibling packages easily. Say I have an engine
> called foo, with a foo.math module and a foo.physics module, you can
> access the math module from the physics module through
> require(_PACKAGE..".math"). Example:
> 
> -- foo/math.lua
> module(..., package.seeall)
> function poly(coefs, x)
>    local fx = 0
>    for i,coef in ipairs(coefs) do
>        fx = fx + coef * x^i
>    end
> end
> --------
> -- foo/physics.lua
> module(..., package.seeall)
> local math = require(_PACKAGE.math)
> function g(x)
>    return math.poly({1,2,3}, x)
> end
> --------

A little off the topic, but this is a bit faster (the coeffs are assumed to be
in decreasing power order):

-- foo/math.lua
module(..., package.seeall)
function poly(coefs)
  return function(x) -- Horner's evaluation
    local fx = 0
    for i, coef in ipairs(coefs) do
      fx = fx * x + coef
    end
    return fx
  end
end
--------
-- foo/physics.lua
module(..., package.seeall)
local math = require(_PACKAGE.math)
g = math.poly{1,2,3} -- x^2+2*x+3

Cheers,
Luis.

-- 
A mathematician is a device for turning coffee into theorems.
        -- P. Erdos 

-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>