Last active
April 7, 2016 21:01
-
-
Save 6ewis/2f06f61b86c42035bd8848d69a495d62 to your computer and use it in GitHub Desktop.
(contains-key :a {:v 1 :b 3 :a 3 :g 6})
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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