Skip to content

Instantly share code, notes, and snippets.

@brianfay
Created November 10, 2015 01:56
Show Gist options
  • Save brianfay/b3894987a02686965080 to your computer and use it in GitHub Desktop.
Save brianfay/b3894987a02686965080 to your computer and use it in GitHub Desktop.
Not actually clojure for the brave and true chap 4 ex 3
;;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