如何在Clojure / Script中迭代嵌套的PersistentArrayMap

时间:2017-03-02 08:29:07

标签: data-structures clojure iteration clojurescript destructuring

对数据库执行查询后,功能的返回是地图列表:

({:id 1 :name "Book 1" :category "Drama"}
 {:id 2 :name "Book 2" :category "Drama"}
 {:id 3 :name "Book 3" :category "Poetry"}
 {:id 4 :name "Book 4" :category "Poetry"}
 {:id 5 :name "Book 5" :category "Fantasy"}
 {:id 6 :name "Book 6" :category "Fantasy"}
 {:id 7 :name "Book 7" :category "Fantasy"}
 {:id 8 :name "Book 8" :category "Science fiction"}
 {:id 9 :name "Book 9" :category "Science fiction"}
 {:id 10 :name "Book 10" :category "Science fiction"}
 ...)

因此,我按类别对数据进行分组,group-by函数返回一个持久数组映射包含strs键和映射向量作为val:

{"Fantasy" [{:category "Fantasy", :name "Book 5", :id 5} 
            {:category "Fantasy", :name "Book 6", :id 6} 
            {:category "Fantasy", :name "Book 7", :id 7}], 
 "Drama" [{:category "Drama", :name "Book 1", :id 1} 
          {:category "Drama", :name "Book 2", :id 2}], 
 "Poetry" [{:category "Poetry", :name "Book 3", :id 3} 
           {:category "Poetry", :name "Book 4", :id 4}], 
 "Science fiction" [{:category "Science fiction", 
                     :name "Book 8", 
                     :id 8} 
                    {:category "Science fiction", 
                     :name "Book 9", 
                     :id 9} 
                    {:category "Science fiction", 
                     :name "Book 10", 
                     :id 10}]}

接下来,我这样做:

(doseq [[k [{:keys [id name]} v]] data]
  (println k)
  (println id name))

副作用是:

Drama
1 Book1
Poetry
3 Book3
Fantasy
5 Book5
Science fiction
8 Book8

doseq仅为每个键返回一个值

如何获取其余值? 结果必须是:

Drama
1 Book1
2 Book2
Poetry
3 Book3
4 Book4
Fantasy
5 Book5
6 Book6
7 Book7
Science fiction
8 Book8
9 Book9
10 Book10

1 个答案:

答案 0 :(得分:0)

你可以像这样做一个内循环:

(doseq [[k vs] data]
  (println k)
  (doseq [{:keys [id name]} vs]
    (println id name)))

或使用单doseq

(doseq [[k vs] data
        {:keys [id name] :as v} vs]
  (when (= v (first vs))
    (println k))
  (println id name))

还有一种脏方法可以打印外环字符串一次:

(doseq [[k vs] data
        :when (or (println k) true)
        {:keys [id name] :as v} vs]
  (println id name))

甚至是这样:

(doseq [[k vs] data
        {:keys [id name] :as v} (do (println k) vs)]
  (println id name))