lua-users home
lua-l archive

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


David Given writes:

> 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)()

I was considering |x| as sugar for function(x).  That makes a lot of these
closure-based control structures more friendly to type.  I find it a little
easier to read because often stuff fits on one line; your taste may vary.

({
	[0] = || print(0) end,
	[1] = || print(1) end
}[value] or ||  print("default") end)()

OK, the last part is a pain, but you knew that anyway.

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

Ooh, I forgot about that.  I suppose:

local st1
function foo()
  st1 = st1 or {[0] = etc}
end

where I guess "st" means "static" to my brain.

Jay