在 Clojure 集中使用重复值

Using Duplicate Values in a Clojure Set

我在使用 clojure 中的集合时遇到问题,其中似乎从集合中删除了重复值,这让我很头疼。

我有一些卡片。

(def cards
  {
    :card1 {:name "Wisp"              :type "Monster"     :damage 1   :health 1   :cost 0 :ability 0}
    :card2 {:name "Spider Tank"       :type "Monster"     :damage 3   :health 4   :cost 3 :ability 0}
    :card3 {:name "Boulder Fist Ogre" :type "Monster"     :damage 6   :health 7   :cost 6 :ability 0}
    :card4 {:name "Bloodfen Raptor"   :type "Monster"     :damage 3   :health 2   :cost 2 :ability 0}
    :card5 {:name "Chillwind Yeti"    :type "Monster"     :damage 4   :health 5   :cost 4 :ability 0}
    :card6 {:name "Magma Rager"       :type "Monster"     :damage 5   :health 1   :cost 3 :ability 0}
    :card7 {:name "War Golem"         :type "Monster"     :damage 7   :health 7   :cost 7 :ability 0}
    :card8 {:name "Oasis Snapjaw"     :type "Monster"     :damage 2   :health 7   :cost 4 :ability 0}
    :card9 {:name "River Crocolisk"   :type "Monster"     :damage 2   :health 3   :cost 2 :ability 0}
    :card10 {:name "Murloc Raider"    :type "Monster"     :damage 2   :health 1   :cost 1 :ability 0}
    :card11 {:name "Northshire Cleric":type "Monster"     :damage 1   :health 3   :cost 1 :ability 2}


    }
 )

这些卡片是一组的一部分。

 (def board1 (set (map cards '(:card3 :card4 :card11 nil nil nil nil))))

当我 return 这套牌时,我想看四张零牌和三张牌。相反,我得到:

#{nil {:ability 0, :name "Bloodfen Raptor", :type "Monster", :damage 3, :health 2, :cost 2} {:ability 0, :name "Boulder Fist Ogre", :type "Monster", :damage 6, :health 7, :cost 6} {:ability 2, :name "Northshire Cleric", :type "Monster", :damage 1, :health 3, :cost 1}}

我想要重复值的原因是我有两张相同的卡片或多个零值。我使用 doseq 循环遍历这些值,当存在​​重复项时 return 出现越界异常。

根据定义,Clojure 集是唯一的。

Returns a set of the distinct elements of coll.

https://clojuredocs.org/clojure.core/set

如果您需要一个非唯一集,则可以使用向量或列表。

https://clojuredocs.org/clojure.core/vec

https://clojuredocs.org/clojure.core/list