What's the weirdest code style you can think of?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

What's the weirdest code style you can think of?

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

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

Re: What's the weirdest code style you can think of?

Post 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
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: What's the weirdest code style you can think of?

Post 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?

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

Re: What's the weirdest code style you can think of?

Post by szensk »

Code: Select all

Hi  1
Hi  2
Hi  3
Hi  4
Hi  5
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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: What's the weirdest code style you can think of?

Post 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 :P

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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: What's the weirdest code style you can think of?

Post 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. ;)
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: What's the weirdest code style you can think of?

Post 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.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: What's the weirdest code style you can think of?

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: What's the weirdest code style you can think of?

Post 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... :crazy:
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest