I am writing a game in Clojure, and I often need some functions to return 
whether they succeeded in doing what they are supposed to do (for example, 
moving that unit one field to the left may fail due to... I don't know, a 
wall?).

But if there is one thing I really like about LISPs is the idea of pure 
functions. So, most of those state-manipulating functions like moving 
really just return a new world state. Which may or may not be the same as 
the previous one, depending on the circumstances.

I see several ways to achieve what I want, that is to return both the new 
world state and success status.
1) Compare the old and new value - doesn't seem very efficient.
2) Return [new-state success?] instead of just new-state - requires too 
much acrobatics from the user of the function (which is still me, but 
whatever).
3) def some atom that will be set accordingly - I thought this was a step 
in the right direction, but... global mutable state? Yuck.
4) Allow an optional last argument to world-modifying functions which is an 
atom that gets set to the success value. This is what I ultimately settled 
for, and I think it is somewhat elegant. That way I minimize the side 
effects to a level I can fully control.
I currently have a function called safe-reset! which is basically (and a 
(reset! a v)), so I don't have to type it out every time myself, and all 
functions that have that optional last argument simply call themselves with 
an additional nil.

Now, my question is - is there some neater scheme for doing this? And if 
not, then at least I hope my idea is helpful to someone. :)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to