[Haskell-cafe] Observer pattern in haskell FRP

Ertugrul Söylemez es at ertes.de
Mon Dec 10 16:56:51 CET 2012


Nathan Hüsken <nathan.huesken at posteo.de> wrote:

> I put a pseudo C++ example below the mail. I use the terms "model" and
> "view" for the game logic and rendering respectively.
> The example is a little different. Asteroids explode when they
> collide. The moment asteroids explode, they are removed from the model
> (the game logic) while in the view (rendering) they still exist until
> the explosion animation is over.
>
> As you said, this basically is sending messages from the Model (in the
> observer pattern called Observable) to the view (Observer). The main
> difficulty I have is how to send the messages from the correct model
> to the correct view.
> In C++ this is done by keeping pointers.
> Simply assigning IDs would work, but than I would have to always pass
> a map from the model to the view, and I feel like (also I have little
> experience with this), that this approach is not very scalable.

Actually it is very scalable, as the same map is passed to every object.
It can even live in the underlying monad, which means that you could
even use a mutable vector, if you wish; however, I don't recommend that.

Remember that a map is immutable and shared, so passing the same map to
multiple objects is in fact the same as passing a pointer in C++.
Lookups in and updates to the map are of logarithmic complexity, so this
scales well.  Only doubling the number of nodes actually adds one full
step to lookups and updates.

If you're dealing with millions of objects you may want to use the
vector solution mentioned earlier.  This requires an imperative
underlying monad, but you would get about the same speed as in C++.


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121210/06b296a3/attachment.pgp>


More information about the Haskell-Cafe mailing list