clojurecademy / 4Clojure

4Clojure problems with a few differences
https://clojurecademy.com/courses/17592186045426/learn/overview
8 stars 2 forks source link

26/32 'a nil key' #25

Open AlexanderChenNL opened 6 years ago

AlexanderChenNL commented 6 years ago

I for the live of me don't understand what the assignment is about, please help :)

ertugrulcetin commented 6 years ago

:) hi here is the iff doc: http://en.wikipedia.org/wiki/If_and_only_if And this is the answer: (fn [a b] (nil? (b a 0)))

touxstone commented 6 years ago

Hi, I got some lost at this question (got already read that about the iff table linked above). I feel instead, info about the (nil? [x]) function or usage (e.g. in this case (as I see it used here as a kind of side effect)) would be of great help but not found so far..

So, something is missing in this one (the 26/32). As the exercise query reads as follow:

Write an anonymous function which, given a key and map, returns true iff the map contains an entry with that key and its value is nil.

My answer (& I find that (the) [other|previous] answer answers other query) is (fn [a b] (= nil (#_get a b))) or (fn [a b] (nil? (#_get a b)))

;; (also got that it's just too close to literals but still under [training|beginner] 
;; purposes though I'm currently unable of testing the first condition of the exercise, 
;; the `and` 
;; for whether or not 
;; the given `key` exist in the given `coll` bit )
touxstone commented 6 years ago

OK I found this another answer:

(fn [%1 %2] (and (contains? %2 %1) (nil? (%2 %1))))`

Lessons I got now onward:

To Do

I still do not know how-to these other solutions:

  (fn [a b] (nil? (b a 0)))  ;; posted above

or this another one, work:

#(nil?(%2 %1 0))

Any explanation would be so greatly appreciated.

ertugrulcetin commented 6 years ago

hi @touxstone , thanks for reaching out. that's a tricky one :)

It should return true if two things are true. That the collection contains the key, and the key is nil

b indicates the map a indicates the key and the third argument which is 0 is the default value when (b a)returns nil so (:key {:bb 1} 0)will produce 0. if it was like that (:key {:bb 1})it returns nil.(this the argument for the last test assertion in the site)

I hope this helps if not please let me know I'll elaborate as much as I can.

touxstone commented 6 years ago

Hi, @ertugrulcetin just a short reply —Ah ha! Thank you, so much!

;; Though, I'd highlight (got it in my priv notes) this exercise as:
;; smart usage of `the default value functionality` e.g. for `get` 
;; (& probably some other primitive functions)