How does functional programming apply to simulations?

后端 未结 6 614
情深已故
情深已故 2021-01-31 05:11

Besides the general question in the title,

  • How do functional programmers and functional languages approach the domain of simulations, which seem to be most natural
6条回答
  •  你的背包
    2021-01-31 05:42

    I'm writing a game in Clojure, using a mostly functional style. For example, the entire game state is modelled as an immutable data structure.

    This has required some slightly convoluted coding. For example, you frequently end up creating functions with a lot of parameters to pass around various elements of game state and ensure that application of game state updates happens to the very latest game version.

    But it has also thrown up some really nice advantages, for example concurrency has proved pretty trivial and you can do fun things like cloning the entire game state to run different simulations in the AI.

    Overall, I'm delighted with Clojure as a language for simulations / games.

    Based on this experience, the things I think would improve Clojure for games / simulations would be:

    • Better support for primitives, especially as function parameters and return values
    • Implementation of core language functions that are less hard on memory allocations (GC pressure is an issue for interactive games!)

    You can see an early version of the game here: Ironclad - Generals of Steam. It's basically a steampunk themed strategy game.

提交回复
热议问题