[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Packaging and importing
- From: Rici Lake <lua@...>
- Date: Thu, 18 Aug 2005 16:31:42 -0500
On 17-Aug-05, at 1:20 PM, Chris Marrin wrote:
I see it most often for "standard" things like Math. Rather than
importing each one separately you can bring in all the functions, like
this maybe:
import("Math","all")
local r = min(1,log(sin(PI*2*fraction)))
which looks a lot nicer than:
require"Math"
local r = Math.min(1,Math.log(Math.sin(Math.PI*2*fraction)))
Call me slow, but I just noticed that in lua 5.1work6, you can use:
str:sub(2, 2)
instead of
string.sub(str, 2, 2)
Oddly (perhaps), lmathlib does not add itself to the metatable for
numbers, but there's no obvious reason why it shouldn't, allowing this
lovely syntax:
> pi = require"math".pi
> fraction = 0.4
> =(1):min( (pi*2*fraction):sin():log() )
-0.53139361498122
OK, it was (mostly) a joke. But for what it's worth, here's the patch
(code copied from lstrlib.c):
----------------CUT HERE------------------------
--- src/lmathlib.c Fri Mar 4 13:57:03 2005
+++ src/loomathlib.c Thu Aug 18 15:36:29 2005
@@ -244,12 +244,23 @@
{NULL, NULL}
};
+static void createmetatable (lua_State *L) {
+ lua_newtable(L);
+ lua_pushnumber(L, 0);
+ lua_pushvalue(L, -2);
+ lua_setmetatable(L, -2);
+ lua_pop(L, 1);
+ lua_pushvalue(L, -2);
+ lua_setfield(L, -2, "__index");
+ lua_pop(L, 1);
+}
/*
** Open math library
*/
LUALIB_API int luaopen_math (lua_State *L) {
luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
+ createmetatable(L);
lua_pushnumber(L, PI);
lua_setfield(L, -2, "pi");
lua_pushnumber(L, HUGE_VAL);
----------------CUT HERE------------------------
This does have the consequence that:
> =fraction.pi
3.1415926535898