Page 1 of 1
What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 3:17 am
by iPoisonxL
The weirdest, yet still functioning way you can code in Lua... Me it'd be something like this little "hi, world" program
Code: Select all
function hi()l={"h";"i";}io.write(l[1]..""..l[2]);io.write", world";end;hi()
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 3:39 am
by szensk
Something like this:
Code: Select all
debug.setmetatable(0, {__mul=function(j, f)if type(j)=="function"then j,f=f,j end for i=1,j do f(i,j)end return j end})
range = 1, 5
* function (i)
print("Hi",i)
end
* print
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 3:52 am
by iPoisonxL
szensk wrote:Something like this:
Code: Select all
debug.setmetatable(0, {__mul=function(j, f)if type(j)=="function"then j,f=f,j end for i=1,j do f(i,j)end return j end})
range = 1, 5
* function (i)
print("Hi",i)
end
what does this do?
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 4:48 am
by szensk
as for an explanation:
Code: Select all
-- set metatable for the number type
debug.setmetatable(0, { __mul = function(number, func) --override multiply operator to accept a function * number
if type(number) == "function" then
number, func = func, number -- arguments were in backward order
end
for i=1, number do
func(i, number) -- pass in current index and maximum index
end
return number
end})
__unused = print * 5
It is *really* weird. And should never be done.
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 3:06 pm
by Inny
I vote for
intercal here, and it's notions of having a polite program.
A more practical example for Lua is something I've played with recently, where your objects aren't based on classes, but produced via copying in members. There's a name for this design pattern, which I'll let ya'll figure out from the object names
Code: Select all
function extend(entity, component)
for k, v in pairs(component)
if k != '_init' and entity[k] == nil then entity[k] = v end
end
if component._init then component._init(entity) end
return entity
end
function entity(body)
local object = { extend=extend }
if body then
object:extend(body)
end
return object
end
local example_component = {
x = 0, y = 0, w = 0, h = 0
_init = function(self)
print("do stuff here")
end,
}
local Player = entity():extent(example_component)
It's weird as hell, but it's totally becoming a norm throughout the industry.
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 4:02 pm
by Roland_Yonaba
Inny wrote:
-snip-
Code: Select all
function extend(entity, component)
--snip--
if k ~= '_init' then --....
--snip--
end
The non-equality test...fixed.
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 5:09 pm
by iPoisonxL
Roland_Yonaba wrote:Inny wrote:
-snip-
Code: Select all
function extend(entity, component)
--snip--
if k ~= '_init' then --....
--snip--
end
The non-equality test...fixed.
I always wondered why lua used a tilde instead of a exclamation mark for the non equality test.
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 7:29 pm
by Inny
Roland_Yonaba wrote:Inny wrote:
-snip-
Code: Select all
function extend(entity, component)
--snip--
if k ~= '_init' then --....
--snip--
end
The non-equality test...fixed.
Heh! I can't tell you how many times in javascript I end up writing stuff like
$(function() end) and then take a moment to realize why it don't do.
Re: What's the weirdest code style you can think of?
Posted: Sat Dec 21, 2013 7:54 pm
by Roland_Yonaba
Inny wrote:Heh! I can't tell you how many times in javascript I end up writing stuff like $(function() end) and then take a moment to realize why it don't do.
Well, I can share a similar experience...Lots of time, when writing VBA macros (in MsExcel), I declare variables with "local". After struggling with the built-in debugger, I realize that it should be "dim" instead...