Why ('f 1) does deliver nil?

69 views
Skip to first unread message

Johannes

unread,
Oct 26, 2019, 1:05:25 PM10/26/19
to Clojure
... instead of an exception?

Can anyone explain it to me?

Johannes

Andy Fingerhut

unread,
Oct 26, 2019, 1:12:11 PM10/26/19
to clo...@googlegroups.com
Both symbols and keywords, when placed inside of an expression in the first position, e.g. (:my-keyword my-map), or ('some-symbol my-map), behave as functions that "look themselves up" in the map that is the first argument, and if that symbol or keyword is a key in that map, the associated value is returned from the expression.  This is most often used for keywords, but it does also work for symbols, which is what is happening in your expression.  The number `1` is not a map, and both this kind of lookup expression, as well as the function `get`, have always returned nil when the thing-to-be-looked-up-in is not a map or set (or anything else that implements the appropriate lookup interface methods being used in the Java implementation under the covers there).

Some other examples of similar behavior:

$ clj
Clojure 1.10.1
user=> ('f 1)
nil
user=> ('f 1 :not-found)
:not-found
user=> ('f {'f 17 :bar 8})
17
user=> ('f {:bar 8} :not-found-value)
:not-found-value
user=> (get {'f 17 :bar 8} 'f)
17
user=> (get 1 'f)
nil
user=> (get 1 'f :not-found)
:not-found

Andy



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/f2007954-107c-4918-bf0a-6bfd8040ba39%40googlegroups.com.

Faiz Ahmed Mushtak Halde

unread,
Oct 26, 2019, 1:13:12 PM10/26/19
to Clojure
Well, Symbols implement the IFn and if you look at the invoke method, the arg passed to it is considered to be a map (I guess more like an environment it expects in which it will try to look for the symbol)

Anyways, since you've passed in a number, the look up in RT.getFrom leads to a null hence the nil

('f1  {'f1  3}) returns 3 FYI  and ('f 1 'not-found) returns 'not-found

Johannes

unread,
Oct 26, 2019, 4:45:59 PM10/26/19
to Clojure
Thank you for the explanation; until now I associated this behavior only to keywords not not symbols.

Johannes

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 clo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages