Skip to content

Instantly share code, notes, and snippets.

@6ewis
Last active April 7, 2016 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6ewis/2f06f61b86c42035bd8848d69a495d62 to your computer and use it in GitHub Desktop.
Save 6ewis/2f06f61b86c42035bd8848d69a495d62 to your computer and use it in GitHub Desktop.
(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