You also have some formatting issues, and your reducing function always returns true. You basically are creating a lambda, and returning true, nothing else.
If we reformat it, you can see it more easily:
There’s really no need for reduce-kv here, but if you want to use it, you could do:
(defn validate2
[validators suspect]
(reduce-kv (fn[acc k v]
(and acc ;; The previous keys were validated
(contains? suspect k) ;; the suspect contains the key in question
(v (suspect k)))) ;; the value of that key in the suspect validates according to the validator function
true validators)) ;; you want to check all validators, so you reduce over them
(defn validate-name[name]
(print name)
(and (string? name) (> (count name) 3)))
(validate2 {:name validate-name} {:name "a"})
;; => false
(validate2 {:name validate-name} {:name "aaaaa"})
;; => true
Thanks, I have the idea that I could never think of this on my own.
With complex things I still loose the big picture.which was what variable stands for that and how to convert my old code for this one.
(defn validate2
[validators suspect]
(reduce-kv (fn[acc k v]
(and acc ;; The previous keys were validated
(contains? suspect k) ;; the suspect contains the key in question
(v (suspect k)))) ;; the value of that key in the suspect validates according to the validator function
true validators))
(defn validate-name [name]
(and (string? name) (> (count name) 3)))
(defn validate-glitter[index]
(and (number? index)) (< index 20))
(validate2 {:name validate-name, :glitter-index validate-glitter } {:glitter-index 10})
it seems to work on all cases I can image and it seems to work
So many thanks for the lessons and the the patience with me.
I hope im a good student.
One cool thing shown chapter is that map can receive a collection of functions, which you can apply to the same argument thanks to an anonymous function. It can be used for a validator that picks up what validate from a collection of validators/functions. Here is the example for this approach from the book, which is not the answer, but it not a spoiler either: