Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(contains-key :a {:v 1 :b 3 :a 3 :g 6})
(boolean (seq (filter #(= k (key %)) m)))
(defn contains-key [m k]
(loop [[entry & rest :as coll] (seq m)]
(if (empty? coll)
false
(or (= (first entry) k)
(recur rest)))))
;;first implementation
(defn contains-key [key hashmap]
(if (= key (first (keys hashmap)))
true
(if (empty? (keys hashmap))
false
(recur key (rest hashmap))
)
)
)
;;second implementation
(defn contains-key [key hashmap]
(if (= key (first (keys hashmap)))
true
(if (empty? (keys hashmap))
false
(recur key (rest hashmap))
)
)
)
;;third implementation
defn contains-key [seek-key hashmap]
(loop [newhashmap hashmap]
(let [[k, _] (first newhashmap)]
(if (= seek-key k)
true
(if (nil? k)
false
(recur (dissoc newhashmap k))
)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.