braveandtrue

https://www.braveclojure.com/
2018-12-14T12:28:22.050200Z

@anantpaatra Yeah, it looks to me like the function description is somewhat ambiguous, for the reasons you mention. Your code looks mostly good, in that it shows you’ve understood what the lesson is trying to teach. In your first answer, there’s a subtle bug in the append, which you will see if you pass in a suspect that fails validation. Also, you probably want if there rather than cond, since you’re only checking one predicate clause.

2018-12-14T12:29:22.051100Z

Your valid? function looks fine, but if I might make a stylistic suggestion: I would probably write it like this:

(defn valid?
  [validator record]
  (let [name-valid? (:name validator)
        glitter-index-valid? (:glitter-index validator)]
    (and (name-valid? (:name record))
         (glitter-index-valid? (:glitter-index record)))))

2018-12-14T12:33:53.054200Z

This is a style I think of as “executable documentation.” By using let to give expressive names to the validation functions, it becomes easier to understand what the code is doing just by reading the code. Compare that to ((:name validator) (:name record))--it’s perfectly legitimate Clojure, and experienced Clojure devs can probably see right away what it’s doing. Maybe. For me, it takes a little bit of extra effort to parse out that (:name validator) must be pulling a validation function out of the validator map because (:name validator) is the first item in the list.

💯 1
2018-12-14T12:34:56.055100Z

Probably just a matter of personal experience and preference, but when I go back and look at my old code, I find the let style much easier to read, and thus much faster.

2018-12-14T12:38:12.056800Z

Also, in Clojure, the core functions like assoc and conj and so on have a convention that the collection comes before the thing you’re adding, so it might be a good idea to write your append function to take [list-of-suspects new-suspect]

👍 1
2018-12-14T12:44:08.058500Z

Your second approach looks like it’s working as intended (and that’s a clever use of and to get it to return the unchanged record if everything else passes 🙂), but you probably don’t want to do it that way, since that could end up giving you a list of suspects that nothing but falses.

felipebarros 2018-12-14T21:20:47.060200Z

I completely agree with everything you said and learned from it. Using let like that makes complete sense and the end result is elegant, I'll try to incorporate that from now on.

felipebarros 2018-12-14T21:22:10.061200Z

The convention on the order of arguments also makes total sense and I even felt something weird with that function but couldn't see what it was.

felipebarros 2018-12-14T21:24:14.062500Z

So you don't think that there is a "clojurian" way to interpret what that return value should be (since it is ambiguous)?

felipebarros 2018-12-14T21:30:06.065600Z

I read "idiomatic" everywhere in the Clojure world so maybe I just try too hard to understand what people mean by it. But the book is quite interesting now and things are flowing, I'm way in the fifth chapter and I guess from now on the meaning of that will just become clearer.

2018-12-14T21:31:21.066300Z

Yeah, “idiomatic” mostly means “people who have been doing this for a while know the best tricks” 🙂

😄 1
2018-12-14T21:32:01.066800Z

But in this case I think the way the exercise was defined was perhaps a bit unclear.

felipebarros 2018-12-14T21:32:52.067400Z

I see 🙂

felipebarros 2018-12-14T21:33:40.068200Z

Thank you again for your time haha Clojurian channels can be very quiet (this intimidated me for a long time) so I hope I'm asking relevant questions.

2018-12-14T21:34:12.068600Z

Sure, any questions you’d like an answer for are relevant to this channel

2018-12-14T21:34:42.069200Z

Well, I mean apart from “How can I make a million dollars?” or “What’s your credit card number?”

2018-12-14T21:34:53.069400Z

😉

felipebarros 2018-12-14T21:35:36.069600Z

😆