Clojure减少/计数集合中的元素数

Clojure减少/计数集合中的元素数,clojure,Clojure,我有这个功能: (defn movies [directors] (->> directors (mapcat :movies) (group-by :genre))) (defn movies [directors] (->> directors (mapcat :movies) (group-by :genre) (map #((juxt key (comp count val)) %))

我有这个功能:

(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (group-by :genre)))
(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (group-by :genre)
       (map #((juxt key (comp count val)) %))))
这给了我这个输出:

{"Action" [{:title "Blade Runner 2049", :genre "Action", :year 2017}],
 "Drama"
 [{:title "Gladiator", :genre "Drama", :year 2000}
  {:title "Apocalypse Now", :genre "Drama", :year 1979}
  {:title "The Departed", :genre "Drama", :year 2006}
  {:title "The Aviator", :genre "Drama", :year 2004}],
 "Adventure" [{:title "Mars", :genre "Adventure", :year 2015}],
 "Crime"
 [{:title "American Gangster", :genre "Crime", :year 2007}
  {:title "The Godfather", :genre "Crime", :year 1972}
  {:title "GoodFellas", :genre "Crime", :year 1990}],
 "Comedy"
 [{:title "Jack", :genre "Comedy", :year 1996}
  {:title "The Wolf", :genre "Comedy", :year 2013}]}
我想在同一个函数“movies”中使用reduce或count来计算每个类别中的电影数量

谢谢,
R.

你真的需要在那里使用
reduce
?因为您可以使用
map
频率的组合来计算项目:

(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (map :genre)
       frequencies)

您真的需要在那里使用
reduce
?因为您可以使用
map
频率的组合来计算项目:

(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (map :genre)
       frequencies)

这只需计算每个
:流派
组中的数字即可:

(map #((juxt key (comp count val)) %) your-current-output)
;; => (["Action" 1] ["Drama" 4] ["Adventure" 1] ["Crime" 3] ["Comedy" 2])
因此,对于您的功能:

(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (group-by :genre)))
(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (group-by :genre)
       (map #((juxt key (comp count val)) %))))

这只需计算每个
:流派
组中的数字即可:

(map #((juxt key (comp count val)) %) your-current-output)
;; => (["Action" 1] ["Drama" 4] ["Adventure" 1] ["Crime" 3] ["Comedy" 2])
因此,对于您的功能:

(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (group-by :genre)))
(defn movies [directors]
  (->> directors
       (mapcat :movies)
       (group-by :genre)
       (map #((juxt key (comp count val)) %))))

嗨,奥列格,像这样使用它,它总是给我1,这是数字:{[“动作”[{:标题“刀锋杀手2049”,:流派“动作”,:2017}]1,[“戏剧”[{:标题“角斗士”,:流派“戏剧”,:2000}{:标题“现在的启示录”,:流派“戏剧”,:1979}{:标题“逝者”,:流派“戏剧”,:2006}{:标题“飞行员”,:流派“戏剧”,:year 2004}]]1,@razvan,似乎你仍然在按流派对电影进行分组。你需要删除对
分组的调用(如我的示例中所示)Hi Oleg,使用它时,它总是给我1,这是数字:{[“动作”[{:标题“刀锋杀手2049”,:流派“动作”,:year 2017}]]1,[“戏剧”[{:标题“角斗士”,:类型“戏剧”,:2000}{:标题“现在的启示录”,:类型“戏剧”,:1979}{:标题“逝者”,:类型“戏剧”,:2006}{:标题“飞行员”,:类型“戏剧”,:2004}]1,@razvan,似乎您仍在按类型对电影进行分组。您需要删除对
分组的调用(如我的示例所示)