
I thought I would give a concrete example of what immutable data can give you.
Instead of an abitary example we will use a RogueLike game world. So I will reuse the game world from my toy [RogueLike](https://steemit.com/programming/@woz.software/building-a-roguelike-in-f-from-scratch-command-logging) I wrote about a while back. See the summary at the end for all the links :)
So what we have is a data structure that defines the game world. It's immutable due to the language used, F#. Basically a simple structure with all the game items. For Map think Dictionary in C# and the rest should make sense.
```
type Level =
{
playerId: ActorId
map: Map;
doors: Map<Vector, Door>;
actors: Map<ActorId, Actor>
items: Map<ItemId, Item>
mapActors: Map<Vector, ActorId>
mapItems: Map<Vector, List<ItemId>>
}
```
- map is the tiles of the world.
- doors are the world doors indexed by coordinates
- actors are actors indexed by their id
- items are items indexed by their id
- mapActors and mapItems are joins to location coordinates
When we run a game turn we rebuild the parts that have changed only and keep everything else. This results in a new level object.
So now we have a references to the structure both before and after the change. The original is untouched and the overall costs in memory is the changed parts of the structure only.
If that does not make sense quickly read through [Immutable collections in C#](https://steemit.com/programming/@woz.software/immutable-collections-in-c) as it details how an immutable binary tree is updated.
You should now be able to see we are able to keep the reference to the world for each turn in a collection. This gives us a cheap and very simple technique to keep the complete game history with low memory overheads. You could then provide an epic game replay feature.
If you were to try this with mutable data you have a problem. You are soon into either cloning the entire structure each turn or coming up with a way to store the deltas. More code and more complexity.
The immutable data, because of its nature and how we can reuse objects, gives us that game history for almost free. That is in terms of complexity and overheads.
Does it require more work?
Not really, just different work and ways of solving problems. But the more you work with immutable data the more you see ways to use it.
Feel free to ask any questions
Oh, and the skull... you are here reading this so it worked :)
Happy coding
Woz