This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-01-19
Channels
- # aatree (33)
- # admin-announcements (70)
- # alda (6)
- # aleph (2)
- # announcements (6)
- # aws (7)
- # beginners (40)
- # bitcoin (1)
- # boot (138)
- # cider (24)
- # cljs-dev (9)
- # cljsjs (18)
- # cljsrn (35)
- # clojars (4)
- # clojure (211)
- # clojure-art (4)
- # clojure-austria (2)
- # clojure-hamburg (8)
- # clojure-russia (66)
- # clojure-sg (3)
- # clojured (1)
- # clojurescript (73)
- # cursive (9)
- # datomic (124)
- # dirac (8)
- # editors (3)
- # emacs (13)
- # euroclojure (10)
- # hoplon (207)
- # jobs (4)
- # ldnclj (27)
- # lein-figwheel (3)
- # leiningen (10)
- # mount (5)
- # music (1)
- # off-topic (9)
- # om (92)
- # onyx (36)
- # perun (30)
- # proton (47)
- # re-frame (11)
- # reagent (11)
- # ring-swagger (7)
- # yada (2)
@chadhs: I think it makes a lot of sense if you are conditionally setting up global stuff based on your environment. See, for example, this bit from cljs.core
: https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L278-L281
interesting thanks. i’m conditionally setting a value to use in the rest of the body of a function and it seemed like a decent approach.
> top level def
s are extremely rare
are they?
I think a general rule of thumb is to only use def
as a top-level form but sometimes (rarely) there are valid reasons not to do so
@chadhs: You probably just want to conditionally set a local and use it in the fn, right?
(let [x (if (> num1 num2) num1 num2)]
(do-stuff-with x))
This is more idiomatic. It introduces a local binding of the result of (if (> num1 num2) num1 num2)
to x which is discarded after the (let ...)
closes
in the case of using def
the defined thing would later still be available outside of the current scope; it is added to the global namespace.
Here is another example where a non-top-level def is useful: It does a one-time check for a bug, and does an appropriate defn
that works around the bug if it is detected https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L782-L793
@mfikes: so the takeaway is: if you need to conditionally set something to be accessible globally and have good reason to do so?
The intent there is to really do one def
, but what gets def
’d is conditional. You could put the if
inside the def
if you really wanted to, but with defn
, perhaps the link above is cleaner
I think for beginning programming with Clojure the takeaway you gave is good 👍
When you come across a situation where you think a non-top-level def
would be appropriate you can always come back here and ask for people's thoughts.
thanks so much guys, the clojure community has been awesome so far 😎 :thumbsup::skin-tone-3: 🚀
I'm trying to get using the key and receiving a nil, even though I can see that the value I want is present
{:orgbid #uuid "0b914bf4-46a6-42ba-8255-3980209b3eaa"} if I do (:orgbid mymap) returns nil