Author Topic: Polymorphism in Lua  (Read 3018 times)

0 Members and 1 Guest are viewing this topic.

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
  • We are the Knights who say... NI!
Re: Polymorphism in Lua
« Reply #15 on: August 19, 2011, 06:17:42 PM »
Pilchard is pretty much right.

Something is a (kind of) class with some stats(x, y, hp, hunger), with their respective retrieving methods(since we're talking about OOP), and CreateSomething(n) creates n instances(objects) of the Something class with mostly random numbers...

Wrapping it up - Someone trying to hack OOP into Lua,  and as far as I can see...succeeding.

Quote
"Creature1.X"

Code: [Select]
Creature1 = {X = 50, Y = 30}
You can now call Creature1.X and Creature1.Y

Want to create a single instance of creature without that much work:

Code: [Select]
function CreateSingleCreature(x, y) do
   return {X = x, Y = y}
end

Want to create several?

Code: [Select]
function CreateSeveralCreature(num) do

   CreaturesArray = {}

   for i = 1, num do
      CreaturesArray[i] = CreateSingleCreature( math.random(0, 50) , math.random(0, 50) )
   end

end

Now you have a array with num creatures.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #16 on: August 19, 2011, 06:24:59 PM »
I guess arrays is more useful in Lua than elsewhere :)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #17 on: August 19, 2011, 06:44:58 PM »
What about matrices?

Example:

CreatureColour[ID].R

How do you set that up?

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
  • We are the Knights who say... NI!
Re: Polymorphism in Lua
« Reply #18 on: August 19, 2011, 07:14:20 PM »
Code: [Select]
function CreateSingleCreature(x, y, r, g, b) do
   return {X = x, Y = y, Color = {R = r, G = g, B = b} }
end

You mean this?

You can get get for example the Red element like this:
Code: [Select]
randomCreature = CreateSingleCreature(fill it all with random values)
randomCreature.Color.R

I guess arrays is more useful in Lua than elsewhere :)

I've only started programming this week, but I personally dislike Lua's array/table system.
While Lua's table is a one big swiss army knife(Nothing wrong with that, just personal opinion), for example Python has everything well separated, and each tool serves for different purpose.
Maybe with time...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #19 on: August 19, 2011, 07:23:17 PM »
Only a week ago??
You're catching up info fast then, I've been sitting for 7 months soon :)

But Lua's array system is very efficient, it can be infinite, atleast thats what I've been getting... Also, with matrices it's super effective catching up things that lazy people would skip :P

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
  • We are the Knights who say... NI!
Re: Polymorphism in Lua
« Reply #20 on: August 19, 2011, 07:35:55 PM »
Only a week ago??
You're catching up info fast then, I've been sitting for 7 months soon :)

But Lua's array system is very efficient, it can be infinite, atleast thats what I've been getting... Also, with matrices it's super effective catching up things that lazy people would skip :P

^^ Nothing is infinite, and pretty much every programming language now-a-days has an awesome array system(which in 70% of the cases derive from C). You rarely decide to learn a new programming language, because an universal feature is the best implementation. You either get curious with a new language either, because you need it, or like Alan Perlis said: "A language that doesn't affect the way you think about programming, is not worth knowing".

From the last case, languages that stand out: Haskell/Lisp(Some dialects more than others)/J(or APL if you're feeling particularly hardcore).

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 904
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #21 on: August 19, 2011, 11:10:56 PM »
Nothing is infinite
O RLY?

Primes + real integers to name just two.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #22 on: August 19, 2011, 11:28:39 PM »
One more thing is infinite : Dividing by 0 :D

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
  • We are the Knights who say... NI!
Re: Polymorphism in Lua
« Reply #23 on: August 20, 2011, 12:06:05 AM »
Not what I meant ^^. I was actually talking about physical things, but I probably could have made it way more explicit. Sry ;)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #24 on: August 20, 2011, 12:22:15 AM »
The universe is practically infinite, it expands so fast that you can't reach the end...

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 904
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #25 on: August 20, 2011, 10:29:54 AM »
One more thing is infinite : Dividing by 0 :D

That's undefined, which is slightly different. Something infinite has no end, but we don't even know that for certain about n/0.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 21
  • Posts: 1,492
  • They'll eat you next!
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #26 on: August 24, 2011, 07:14:55 AM »
Pilchard, it must be infinite, else some weirdos guys would've used time to calculate it. You never know what people do...

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
  • We are the Knights who say... NI!
Re: Polymorphism in Lua
« Reply #27 on: August 24, 2011, 07:52:10 AM »
Pilchard, it must be infinite, else some weirdos guys would've used time to calculate it. You never know what people do...

Something that is undefined, does not mean it hasn't been discovered yet, it means it has no solutions. Like the intersection of parallel lines.(I can't confirm the relation with this comparison. There's a distinct line between impossible/undefined functions and I get distracted way too easily in math class...)

And yes, most of the time, zero divisions can be seen as infinity, but really...they are not worth the trouble...

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 904
  • Eufloria: Yes
Re: Polymorphism in Lua
« Reply #28 on: August 24, 2011, 12:57:43 PM »
Parallel lines do intersect. They intersect at an infinite distance from any given point. [/mathnerd]

However, n/0 is undefined, but can be treated as infinite in many cases. Lua being Lua, it has various cases where it isn't[/] infinite but undefined and bleh...

http://lua-users.org/wiki/MathLibraryTutorial  The section on math.huge tells you what Lua treats as infinite and what it does not.

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
  • We are the Knights who say... NI!
Re: Polymorphism in Lua
« Reply #29 on: August 24, 2011, 01:37:36 PM »
Actually parallel lines only intersect at infinite distance in Non-Euclidean geometry, not in Euclidean geometry, and even there only parallel geodesics intersect at infinity, since geodesics can also be ultra-parallel(which do not intersect at infinity).
On a additional note, Non-Euclidean geometry, makes your head hurt.