Hi,
I wrote a function to trackdown a path in a vector containing nested maps:
(defn get-files-from-folder-path [ffs folder-path]
(filter #(= :file (:type %))
(loop [tree-path-position 0 acc [] fof ffs]
(let [folder (first (filter #(and
(= :folder (:type %))
(= (nth folder-path tree-path-position nil) (:name %))) fof))]
(if (not (:children folder))
acc
(recur (inc tree-path-position)
(if (= (+ tree-path-position 1) (count folder-path)) (concat acc (:children folder)) acc)
(:children folder)))))))
And these are the inputs:
(def ffs [{:type :folder, :name "sub1", :children [{:type :file, :name "datomic-data.edn"}]}
{:type :folder, :name "sub2", :children [{:type :file, :name "foo (1).csv"}
{:type :folder, :name "sub21", :children [{:type :file, :name "lein-env"}]}]}])
(def tree-path ["sub2" "sub21"])
And I call it like this:
(get-files-from-folder-path ffs tree-path)
Is there a way to use reduce for that? I was stuck because I think I have to reduce on two lists here,
but reduce only takes one to start with, thats why I chose the loop / recur route. Which works
but it looks ugly to me and I am afraid in one week I want understand it anymore.
Thanks Regards,
Sven