Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26/32 'a nil key' #25

Open
AlexanderChenNL opened this issue Dec 7, 2017 · 5 comments
Open

26/32 'a nil key' #25

AlexanderChenNL opened this issue Dec 7, 2017 · 5 comments

Comments

@AlexanderChenNL
Copy link

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

@ertugrulcetin
Copy link
Member

:) 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
Copy link

touxstone commented Mar 12, 2018

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
Copy link

OK I found this another answer:

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

Lessons I got now onward:

  • Usage of %1 %2 for referencing arguments in the function body.
  • Useful specially in functions where the syntax forces to give arguments in a given-by-its-macro order
  • So be able to implement the and wrapping function (as in this case was specially sine qua non).

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
Copy link
Member

ertugrulcetin commented Mar 12, 2018

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
Copy link

touxstone commented Mar 31, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants