I'm currently playing around with Scala development, but I need to integrate with libraries such as box2d to handle physics. The problem is that this requires to depend on an external library which manages its own state. You keep track of the bodies which you pass into the box2d world. To sum up the aspects in play:
So my question is:
How can you keep track of those references in an elegant way (for functional programming) and how can you minimize the effect it has on purity in the rest of your code?
Stuff like state monads are not going to help me here I guess
In pure functional programming, state is manipulated by functions that take some state and return the next state. Sequencing through states is then achieved by passing the state through a sequence of pure functions. Even global mutable state can be modeled this way.
In functional programming, data cannot be stored in objects and it can only be transformed by creating functions. In object-oriented programming, data is stored in objects. The object-oriented programming is widely used by the programmers and successful also.
Functional programming is based on mathematical functions. Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc. Pure Functional Languages − These types of functional languages support only the functional paradigms. For example − Haskell.
The most practical way is to determine what invariants must hold for the impure actions to be encapsulated without leaking the fact there are side effects, and then, once you have evidence that is the case, hiding the state inside of a "unsafePerformIO".
An alternative is to expose the fact there is internal state, by e.g. an explicit 'i have been initialized' token, that is unforgeable, and unsplittable, to guarantee linear access to the underlying resource.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With