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

How to iterate nested PersistentArrayMap in Clojure/Script

对数据库执行查询后,函数的 return 是映射列表:

({: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 函数对数据进行分组 returns 持久数组映射包含 strs 键和映射向量作为 vals:

{"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 return每个键只输入一个值.

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

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

你可以像这样制作一个内部循环:

(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))