Separate out traversal from selection to make this clearer. We make a generic traversal function get-in-via. It accepts a via function which takes the current result and some value which determines the next result, and returns the next result.
(defn get-in-via [m via ks] (reduce (fn [m' k] (via m' k)) m ks)) Here is a via function that follows folder paths. This is a single "step" of the reduction. (defn via-folderpath [items foldername] (->> items (filter #(and (= (:type %) :folder) (= (:name %) foldername))) (first) :children)) Example of use: (get-in-via ffs via-folderpath ["sub2" "sub21"]) ;=> [{:name "lein-env", :type :file}] On Tuesday, March 24, 2015 at 4:22:21 PM UTC-5, Sven Richter wrote: > > 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 > > > > -- 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 Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com 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 clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.