lua-users home
lua-l archive

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


On Wednesday 01 September 2004 14:08, Andreas Falkenhahn wrote:
[...]
> Maybe someone finds this useful. I have not tested it much and it was
> implemented rather quickly, so no guarantee for whatsoever. But if you find
> bugs, please notify me.

I may be being a killjoy, but:

({
	[0] = function ()
		print(0)
	end,

	[1] = function ()
		print(1)
	end
}[value] or function ()
	print("default")
end)()

Admittedly, this will probably generate a fair bit of garbage, but it is O(1).

Is the compiler clever enough to optimise function closures that take no 
parameters and never leave the parent function?

-- 
+- David Given --McQ-+ "There is one thing a man must do // Before his
|  dg@cowlark.com    | life is done; // Write two lines in APL // And make
| (dg@tao-group.com) | the buggers run."
+- www.cowlark.com --+ --- The Devil's DP Dictionary