The `get` function will accept `nil` as the map argument and return `nil`, 
but invoking `nil` will throw a NullPointerException.

By convention, people use `get` when they don't know what the map argument 
is, since it might be nil. And we use keywords as functions when they are 
literals, like (:foo m).

(let [m (...something that might return a map or nil...)]
  (get m "some key"))  ; returns nil or value

(let [m (...something that might return a map or nil...)]
  (m "some key"))  ; might throw exception

All forms of map lookup support a default argument if the key is not found:

user=> (let [m {:a 1 :b 2}] (m :c "not found"))
"not found"
user=> (let [m {:a 1 :b 2}] (:c m "not found"))
"not found"
user=> (let [m {:a 1 :b 2}] (get m :c "not found"))
"not found"

–S


On Tuesday, September 13, 2016 at 7:58:14 AM UTC-4, Rickesh Bedia wrote:
>
> Hi everyone,
>
> I am new to Clojure and have been playing around with it for a little 
> while. I have seen that (get [3 2 1] 0) and ([[3 2 1] 0) both return the 
> value 3. Similarly (get {:a 0 :b 1} :b) and ({:a 0 :b 1} :b) return 1.
>
> I was wondering if anyone could explain why the get function is useful or 
> maybe an example?
>
> Apologies in advance if this question is due to ignorance and I haven't 
> reached the level where this function is used.
>
> Thanks in advance
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to