One side node, *repeatedly* produces a lazy-seq. Since the REPL forces that
seq when it tries do print everything you get the desired results, but if
you were to
(def x (repeatedly 1000 addmod)
and then inspects mobs it would be empty, since no one forced the seq. If
you want to force side-effects (alter the ref), use
(dotimes [_ 1000] (addmob))
It has the added benetfit of returning nil, so nothing is printed.
HTH,
/thomas
On Wednesday, July 17, 2013 10:50:42 AM UTC+2, vis wrote:
>
> Hello everyone,
> I have a ref, which is a hashmap of monsters (they key is a unique id, the
> value is the monster) like this:
>
> *(defrecord mob [name level hp-cur hp-max])
> (def mobs (ref {}))
> (defn addmob []
> (dosync (alter mobs assoc (gen-id) (->mob "test" 1 2 3)))*
>
> Now if I add 1000 monsters, like this:
> *(repeatedly 1000 addmob)*
> it will take a few minutes, use a lot of RAM and might even fail if its
> not a 64 bit JRE (due to RAM limitations).
>
> As it seems the problem isn't exactly clojure, its the fact that the
> result is being shown in the REPL (which is a huge string).
>
> If I modify my addmob function to return nil:
> *(defn addmob []
> (dosync (alter mobs assoc (gen-id) (->mob "test" 1 2 3))
> nil)
> *Then *(repeatedly 1000 addmob)* is really fast because it doesn't show
> the whole result in the REPL.
>
> *(Please note that the problem has nothing to do with the repeatedly
> function. Even if I already have 1000 mobs in my hashmap and then only add
> another one, it also takes forever to show the result in the REPL.)*
>
>
> Does anyone know a elegant solution to my problem, besides writing
> something like "alter-silent" that always returns nil instead of the whole
> ref? Am I just using things the wrong way? Or could it have something to do
> with Eclipse?
>
> Thanks in advance!
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.