Skip to content

Instantly share code, notes, and snippets.

@egbulmer
Created April 12, 2015 03:51
Show Gist options
  • Select an option

  • Save egbulmer/e44662320b29d1c7b4b4 to your computer and use it in GitHub Desktop.

Select an option

Save egbulmer/e44662320b29d1c7b4b4 to your computer and use it in GitHub Desktop.
Last location in zip function is not being updated.
(defn lprocess
"Process all locations that match the predicate function with the supplied
mapping function. Locations that do not match are unchanged."
[pred func ent]
(->>
(iterate (fn [l]
(if (pred l)
(zip/next (func l))
(zip/next l))) (zip/vector-zip ent))
(take-while (complement zip/end?))
(last) (zip/root)))
(defn component-process [pred func ent]
(lprocess #(and (not (zip/branch? %)) (pred (zip/node %)))
#(zip/edit % func) ent))
(def ent
[{:name :transform :position [0 0] :rotation 0 :scale [1 1]}
{:name :sprite, :file "foo", :ext ".png"}
{:name :sprite, :file "foo", :ext ".png"}
{:name :sprite, :file "foo", :ext ".png"}])
(component-process #(= :sprite (:name %)) #(assoc % :foo :bar) ent)
;; [{:name :transform :position [0 0] :rotation 0 :scale [1 1]}
;; {:name :sprite, :file "foo", :ext ".png", :foo :bar}
;; {:name :sprite, :file "foo", :ext ".png", :foo :bar}
;; {:name :sprite, :file "foo", :ext ".png"}]
;; Last element isn't updated!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment