5 ms·
Though I am not much familiar with functional programming, as far as I tried, one of the hardest problem in functional style is referencing to a mutating object
by eonil 13y ago
Though I am not much familiar with functional programming, as far as I tried, one of the hardest problem in functional style is referencing to a mutating object.
Where an object is immutable, it has to create a new instance of itself for mutation. That invalidates existing references. (if the language support object referencing…) The only solution I can figure out is putting unique tags for each objects, and lookup them for each time. In other words, I have to reference them indirectly which introduces lookup cost. And this cost is usually unacceptable.
If the data structure is pure tree, functional style usually superior on simulations - easier writeup and easier debugging by retaining all the intermediate state. But most games are graph structured. They usually contains many links to arbitrary node which are very hard to be updated together with target object mutations.
If I am wrong, please correct me. I want to use functional styles on games, but I don't have a good solution for referencing problem.
- judk 13y agoYou can look at "FRP". The pure-function approach is to have one outer loop to update state (and all earlier references are frozen, not just some), so there are no references from previous-iteration state to future-iteration state (but the reverse is OK).
- eonil 13y agoI have heard of it. But the problem is data structure is not a pure-tree, so we need arbitrary referencing in a version of state, and FRP doesn't seem to provide a solution for it.
- runT1ME 13y agoHere's a (web based) pure functional version of the game 'bomber man', the readme should answer your questions. https://github.com/vmarquez/PureBomberMan https://github.com/vmarquez/PureBomberMan
- hrjet 13y agoBut that doesn't answer the GP's worry about lookup cost. The thing is, it is hard to keep direct references in a nested data structure (like a graph or tree) even in a language that supports direct references. An example: I want to create a tree in which each node keeps a track of its children as well as its parent. AFAIK, it is impossible to keep track of both children and parent (with direct references) in an immutable data structure.
- Peaker 13y agoThe lookup cost is O(logN). Why do you claim this is "usually unacceptable"? I think it is virtually always acceptable.
- magnusjonsson 13y agoThe constant factor is huge compared to a straight pointer.
- Peaker 13y agoMost parts of most programs aren't performance centric and can take a hit. As evidence, see all the Ruby/Python programs that have constant penalties that go up to 200 in many cases.