[racket] Advice: simulating stateful API in functional setting

From: Nadeem Abdul Hamid (nadeem at acm.org)
Date: Sat Jul 16 00:46:33 EDT 2011

On Sat, Jul 16, 2011 at 12:34 AM, Mark Engelberg
<mark.engelberg at gmail.com>wrote:

> Because the image is not only dependent upon the current location of
> the sprite, but also the entire history of the sprite's movements when
> its pen was down.  So you either have to store a very long list of
> line segments in the world structure, redrawing them every time, or
> alternatively, the world structure is comprised of the current sprite
> info and a picture of what has been drawn thus far.
Ah, right. How about you keep the picture separate, and functions like
'move', 'rotate', etc. add to a list in the sprite structure; when the
sprite structure is passed to the draw handler, the draw handler updates the
picture based on those new segments or whatever, and then clears them from
the sprite's list. This way you don't have to conflate the sprite with the
picture, nor do you have to worry about efficiency w.r.t. redrawing the
entire history of the sprite movement every draw update. Tests for the
'tick' handler can be written in a functional style since only the sprite
structure is involved. ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110716/c64bbc20/attachment.html>

Posted on the users mailing list.