News for hackers. Stuff that matters.
Re:Functional thinking: Why functional programming is on the... by eru on Tuesday January 29, 2013 @08:39PM (#5137596)
> Well, every time you add a new type of entity (or a new data structure to index existing entities), you could add another parameter to the main game loop. But that gets crazy pretty fast. So then you have the clever idea to maintain one massive game state hash, and just pass that around. But wait, now you've lost something key to functional programming: You can no longer tell exactly what aspects of the game state a function is updating, because it just receives and returns the big game state hash. You don't really know what data your functions depend on our modify. Effectively, it's almost like you're using global variables.

You have to explain how that works. By "hash", do you mean a finite map (or dict, in Python terms)?

In Haskell you can use a state monad for these game loops. Some game industry insiders (see http://lambda-the-ultimate.org/node/1277) argue explicitly for this style.

Re:Functional thinking: Why functional programming is on the... by dllthomas on Tuesday January 29, 2013 @10:04PM (#5137813)
A state monad is a decoration of functions that take and return the state. It's certainly prettier, and there are respects in which the parent's complaints are overblown (certainly, it's no worse than imperative languages), but the parent is entirely accurate that a function like

    frobnicateTurboencabulator :: GameState -> GameState
doesn't have a type that tells you anything about what it actually does. Compare that with something like

    on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
where the type tells you basically exactly what the function does.

The state monad doesn't help with this problem:

    frobnicateTurboencabulator :: State GameState ()

Of course, the solution is is some combination of:

1) Split functionality into many typeclasses and hide the State monad, so you can see what functionality is accessed by a function (as mentioned in my other comment),

2) Have functions return a description of updates to the world-state instead of performing those updates themselves (so you know what kinds of things a particular function might do). This might have the benefit of letting you compose actions before applying them, which could in some cases be faster if the updates are likely to be large.

There are likely other options, as well.

Re:Functional thinking: Why functional programming is on the... by eru on Tuesday January 29, 2013 @10:18PM (#5137855)
> Of course, the solution is is some combination of:

And as always in Haskell: more type trickery. Like lenses, which help with the splitting. Applicative functors might help with "Have functions return a description of updates to the world-state [...]" for free in a sense similar to http://gergo.erdi.hu/blog/2012-12-01-static_analysis_with_ap...

Re:Functional thinking: Why functional programming is on the... by dllthomas on Thursday January 31, 2013 @09:41PM (#5148869)
Lenses are awesome, and can help with pulling data out of a WorldState object and updating it thereafter. They do not do a thing about loss of information in the function signature.