Created
November 10, 2015 01:56
-
-
Save brianfay/b3894987a02686965080 to your computer and use it in GitHub Desktop.
Not actually clojure for the brave and true chap 4 ex 3
This file contains hidden or 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
;;I just spent forever and a half writing a function that takes a list of keys and an input map, and validates | |
;;that all keys are present within the input map. I wrote it in two different ways, but they're both ugly as sin. | |
;;Rereading the exercise, I realize that it was actually asking for something different. But whatever, still a useful exercise! | |
;;could probably clarify by making "all-true?" a function, like | |
(defn all-true? | |
[seq] | |
((complement #(some false? %) seq)) | |
;;or maybe a contains-true function (basically just applying or to a sequence) | |
(defn contains-true? | |
[seq] | |
(reduce #(or %1 %2) seq)) | |
(defn validate | |
"Checks if record contains values for all keys in key-map" | |
[key-list record] | |
(reduce #(or %1 %2) | |
(map nil? (map #(% record) key-list)))) | |
(defn validate | |
[key-list record] | |
((complement #(some false? %)) | |
(map #(contains? record %) key-list))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment