2

I'm currently trying to get a value from a list of hash-maps.

  (def cards
  (hash-map
    :card1 {:name "Wisp" :damage 1 :health 1 :cost 0}
    :card2 {:name "Spider Tank" :damage 3 :health 4 :cost 3}
  )

I have a hash-map with my "cards".

(def deck1
      (list (get cards :card1) (get cards :card2) (get cards :card1))

And a deck which is a list of these cards. (I've shortened the two structures)

What I'm trying to do is search this deck structure for a card by passing it a card name. This card is kept as a var and passed elsewhere. I will then reconstruct the list without this card.

So, I'm trying to draw a specific card from anywhere in the deck.

Currently I'm just trying to get the card but I've hit a dead-end with the code.

(defn playCard [card]
(let [c first deck1 (get-in deck1 [card :name]))]
    (println "LOOK AT ME" c)))

Any help would be appreciated.

JT93
  • 461
  • 1
  • 3
  • 13
  • You said you're trying to search `deck1` for a card that has a particular `:name`? Then why does `playCard` take as input a card and not a card name? Assuming that what you're asking for is how to implement `playCard`, could you document more specifically what `playCard` is supposed to do? – Sam Estep Jan 28 '16 at 14:13
  • Take a look at Specter - https://github.com/nathanmarz/specter – Frank C. Jan 28 '16 at 20:09

1 Answers1

2

Here is an example of how to retrieve your cards. I also refactored the declarations :

(def cards {:card1 {:health 1, :name "Wisp", :damage 1, :cost 0}, 
            :card2 {:health 4, :name "Spider Tank", :damage 3, :cost 3}})

(def deck [(:card1 cards)
           (:card2 cards)
           (:card1 cards)])

(defn find-first
     [f coll]
     (first (filter f coll)))

(find-first #(= (:name %) "Wisp") deck) ;; => {:health 1, :name "Wisp", :damage 1, :cost 0}

This is assuming you want to find a card by name (it doesn't make sense to look for a card you have already).

nha
  • 17,623
  • 13
  • 87
  • 133
  • If I wanted to call find-first from another function would I need to make any changes? I'm currently receiving "Don't know how to create ISeq from clojure.lang.Atom". As in my function my deck is defined as an an atom. – JT93 Jan 28 '16 at 15:05
  • `find-first` takes two arguments : a function and a collection. Presumably you are not passing a collection as the second argument, but an atom. Maybe try to dereference it first with a `a` : `(find-first same-fn @your-atom)` – nha Jan 28 '16 at 15:10
  • Thanks, had a look at @ and the function now works as intended. Sorry, one last question, could you point me towards a expression/function that I could utilize to recreate that list without the element that I found. – JT93 Jan 28 '16 at 15:27
  • 2
    There are many ways to do this. First, become familiar with `filter`. Also see http://stackoverflow.com/questions/939278/how-can-i-remove-an-item-from-a-sequence-in-clojure and http://stackoverflow.com/questions/1394991/clojure-remove-item-from-vector-at-a-specified-location for instance – nha Jan 28 '16 at 15:30