Brave book chapter4 and stuck

now I have this part
reduce-kv (fn [vl k v]

where
vl are the validations
k is the key which I want to check for
v is the value so the function that needs to be called to validate the function

vl is not the validator map in that call, it’s an accumulator. Have a look here: reduce-kv - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples

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:

(defn validate2
  [validators suspect]
  (reduce-kv (fn [vl k v]
               #(v (k (vl keys) (keys vl)))
               true)
             false
             suspect)) 

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, Still a lot to learn

Stick with it, it’s worth it. And don’t hesitate to ping me for help :slight_smile:

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.

yep, here the complete code that I make up

(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.

1 Like

Everyone goes through that, just stick with it…

1 Like

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:

(def sum #(reduce + %))
(def avg #(/ (sum %) (count %)))
(defn stats
  [numbers]
  (map #(% numbers) [sum count avg]))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.