On Nov 20, 12:23 pm, islon <[EMAIL PROTECTED]> wrote:
> I'm porting my single thread simple mud-like rpg game from scala to
> clojure and one thing that is annoying me is the code needed to change
> some var.
> In scala I do things like that:
>
> val player = new Player(...)
>
> player.str += 1
>
> in clojure I have to do (player is a struct and its attributes are
> refs):
>
> (def player (Player ...))
>
> (dosync (ref-set player (assoc player :str (inc (:str player)))))
>
> In scala when I want mutability I just define the variable with "var"
> instead of "val", just one letter, simple and elegant.
> It's a game, it's full of states and mutability, the game is single
> thread, I don't need transactions. Am I missing something?
If this is really that big of a deal, you could always go the macro
route. I'm not experienced enough to say if this is a long-term good
decision, but here's a first pass, don't even have Clojure available
to try it, attempt at wrapping up Chouser's suggestion (I think :=
would be better here, but that clashes with keyword definition):
(defmacro |= [target attr fun]
"Updates 'attr' of 'target' by passing the current value to 'fun'
and commuting the result, all inside of a transaction"
`(dosync
(commute ~target update-in [~attr] ~fun)))
With usage:
(|= player :str inc)
That look a little better?
-Adam
--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---