How to remove an item form an collection atom?

15 views
Skip to first unread message

Ricardo Sanchez

unread,
Feb 6, 2014, 2:44:41 PM2/6/14
to clj-pro...@googlegroups.com
I'm storing particles in an atom and I would like to remove them from the collection atom once a condition is met, here is my update-particle function:

(defn update-particle
  [{:keys [position velocity age] :as particle}]
  (let [new-position-x (+ (:x position) (:x velocity))
        new-position-y (+ (:y position) (:y velocity))
        new-age (if (> age 0) (dec age) 0)
        new-is-dead (if (= age 0) true false)]
    (println new-is-dead)
    (assoc particle
      :position {:x new-position-x
                 :y new-position-y}
      :age new-age
      :is-dead new-is-dead)))

So the particle slowly die on each frame by decrementing the "age" local variable and when the age reach 0 the "is-dead" property of the particle is updated to "true" and is at that moment when I need to remove the particle from the atom.

Hope that makes sense, any help will be much appreciated

Nikita Beloglazov

unread,
Feb 6, 2014, 6:23:17 PM2/6/14
to clj-pro...@googlegroups.com
Hi Ricardo

I think you can remove dead particles like this:

(defn remove-dead [particles]
  (remove :is-dead particles))

(swap! particles-atom remove-dead)


Nikita


--
You received this message because you are subscribed to the Google Groups "clj-processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-processin...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ricardo Sanchez

unread,
Feb 7, 2014, 2:21:29 AM2/7/14
to clj-pro...@googlegroups.com
Hey Nikita,

Plug-and-play it works straight away!

Many thanks

Ricardo Sanchez

unread,
Feb 7, 2014, 2:33:11 AM2/7/14
to clj-pro...@googlegroups.com
1 more question, now I have 2 calls to swap! over my particles container atom like this:

(swap! particles-container remove-particles)
(swap! particles-container update-particles)

Is there a way to merge these 2 into a single call over the same atom?

Hope that makes sense

Nikita Beloglazov

unread,
Feb 7, 2014, 10:14:54 AM2/7/14
to clj-pro...@googlegroups.com
Check comp function.


--

Ricardo Sanchez

unread,
Feb 7, 2014, 1:15:46 PM2/7/14
to clj-pro...@googlegroups.com
Works like a charm! I change it to:

(swap! particles-container (comp update-particles remove-particles))

Thanks again!
Reply all
Reply to author
Forward
0 new messages