Good examples/read on handling data?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Good examples/read on handling data?

Post by markgo »

I really enjoy implementing things as objects in Lua. So as you can guess, most things are done through the colon syntax. I was thinking of an alternative where I decouple my data from functions, and I would group similar functions into a module (like Love) and pass the data to them for handling. I can see one advantage of this as not hiding your data from the user behind methods.

Anyways, I would like to know some other programming paradigm/style that I can implement and learn. Lua examples would be nice.
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Good examples/read on handling data?

Post by Hexenhammer »

Lua has pretty good support for functional programming I think. However, I am not really qualified to say much about that given that I have very little experience with that style. But I can show you a basic example

Code: Select all

  -- Imperative style
  for i = 1, #myTable do
    myFunction(myTable[i])
  end

  -- Functional style
  r = map(myTable, myFunction)
map is a basic primitive of functional languages and is a function which applies another function to all elements in a list. Also note that map returns 'r'. In pure functional programming data is immutable by default. So you wouldn't modify myTable itself if you wanted a modified version of it, you would construct a new table from scratch. This has advantages and disadvantages.

There are more basic primitives like map in functional languages e.g. "filter" and "reduce" which are used instead of loops.

Code: Select all

  -- Imperative style
  for i = 1, #t do

    if t[i] < 3 then
      r[#r+1] = t[i]
    end

  end

  -- Functional style
  r = filter(t, function(e) return e < 3 end )
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Good examples/read on handling data?

Post by markgo »

That's interesting. I can see one advantage of keeping your table immutable as variables or functions that reference a table don't behave differently. There wouldn't be implicit changes and instead you have to be explicit in your code and update all functions and variable references to the new table.

Not having loops and using recursion for functional style seems like a pain! I love loops.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Good examples/read on handling data?

Post by miko »

markgo wrote:Anyways, I would like to know some other programming paradigm/style that I can implement and learn. Lua examples would be nice.
I have no lua examples ready to show, but have a look at Entity Systems
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Good examples/read on handling data?

Post by kikito »

You can certainly build objects that don't require "colon syntax". But you will probably not like it.

Look:

Code: Select all

local function Monster(name, age)
  return {
    roar = function() return "I am " .. name .. ", hear me roar!" end,
    drink = function()
      if age < 18 then return "I am too young to drink" end
      return "glub glub glub burp"
    end
  }
end

local peter = Monster('Peter', 12)
local sam = Monster('Sam', 32)

peter.roar() -- I am Peter, hear me roar!
peter.drink() -- I am too young to drink
sam.roar() -- I am Sam, hear me roar!
sam.drink() -- glub glub glub burp
The gist of it is: there's no sharing of functions. Each object has its own "copy" of everything. Believe it or not, this pattern is widely used in javascript. While certainly object creation is slower this way, invoking methods, however, is a bit faster. If you are constantly reusing the same objects, and calling the same functions again and again once they are built, and you don't have thousands of objects, then this pattern should perform well.

This pattern also has the advantage of keeping the properties private (you can't do peter.age = 4 from outside, you must provide a setter function). To be completely honest, I think javascripters use this pattern because their objects are a bit wacky (the javascript metatable equivalent is very limited and not simple to use, their object keys are internally stored as strings, and they have no weak references).
Last edited by kikito on Fri Mar 08, 2013 8:43 am, edited 3 times in total.
When I write def I mean function.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Good examples/read on handling data?

Post by substitute541 »

What kikito said about "you can't do peter.age = 14"... that is why my asteroids seem to be weird in my game(all of my asteroids seem to have merged edges)...
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Good examples/read on handling data?

Post by Inny »

The classic go-to for programming is the Structured/Procedural/Imperitive model, where by you have functions, and structures, and flow control tools, and then that's kind of it. I'm talking, of course, of C and the ALGOL family of languages. In this kind of programming model, your code tends to look like this:

Code: Select all

function create_game()
  local game = {}
  game.player = create_player()
  game.enemy = create_enemy()
  game.goal = create_fat_stack_of_cash()
  return game
end

function play_game(game)
  repeat
    move_player(game.player)
    move_enemy(game.enemy)
  until win_condition(game)
end
Seeing as how C is the most commonly used and long lasting programming language, the model is more than proven to work. The only caveat here is that Lua lacks a switch statement, which is how C generally does dynamic dispatch. You either replicate it via a big if then elseif then else end construct, or by having a table of functions and using lua's table key lookup.

If you decide to do things this way, the major benefit is that you never have to muck around with metatables or object-oriented code, and its always crystal clear what the control flow through the program is.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests