This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # announcements (1)
- # architecture (1)
- # beginners (125)
- # boot (6)
- # boot-dev (2)
- # calva (69)
- # cider (38)
- # cljs-dev (3)
- # clojure (212)
- # clojure-austin (7)
- # clojure-australia (1)
- # clojure-denver (2)
- # clojure-europe (3)
- # clojure-gamedev (1)
- # clojure-hamburg (8)
- # clojure-italy (10)
- # clojure-nl (31)
- # clojure-russia (3)
- # clojure-uk (57)
- # clojurescript (56)
- # core-async (3)
- # cursive (15)
- # datascript (1)
- # duct (28)
- # emacs (6)
- # events (1)
- # figwheel-main (11)
- # fulcro (22)
- # luminus (59)
- # lumo (2)
- # onyx (4)
- # overtone (1)
- # re-frame (1)
- # reagent (4)
- # remote-jobs (3)
- # rum (2)
- # shadow-cljs (84)
- # spacemacs (7)
- # speculative (5)
- # vim (1)
- # yada (127)
When nil should result if a test is false, and only one form is needed when the test is true, is it more idiomatic to use
(when test xxx) or
(if test xxx)? Or is neither really more idiomatic?
Let me try one more time mentioning side effects:
When nil should result if a test is false, and only one form is needed when the test is true and there are no side effects, is it more idiomatic to use
(when test xxx) or
(if test xxx)?
@mark540 Have you looked at what the Community Style Guide has to say about that?
(I wasn't sure what it said, so that was just a suggestion... now I look, it seems to only make a recommendation of
(when cond form1 form2) over
(if cond (do form1 form2)) which, yeah, I'd agree with... but doesn't cover simple
(if cond form) vs
(when cond form)...)
Personally, I would use
(when cond form) instead of an
if without an else form, since that makes it clear (to me) that you are deliberately returning
(not cond) rather than you just accidentally omitted the else form in an
Right, the style didn't say, so I thought I'd see what you all thought. I like that about
when also. But I wasn't sure if side effects were implied. I guess you don't think so.
No, I don't think
when is a good indication of pure functions vs side-effects -- that's far too subtle of a choice, IMO @mark540
Good naming should make that clear I think (have you read Elements of Clojure by Zach Tellman?).
There is a camp that uses a
! suffix to indicate side-effects but that's never really been a consistent convention (and side-effects have a tendency of propagating throughout your program so you'd have! exclamation! marks! everywhere! 🙂 )
Yes. I was thinking of functions like
doseq that are only useful with side effects and probably shouldn't be used when there are none.
when could be thought of in that category since
if is an alternative without side effects, but I think you're right that
when is also used instead of
if for clarity.
But maybe that's because I've been doing Clojure a long time now and my brain automatically sees
when as returning
nil in the non-`cond` case...?
A lot of Clojure's idioms are non-obvious at first, and also depending on what your language background is...
An example where I think
when is much more natural -- when you're constructing a string, using
str, and you have a bunch of conditional parts:
(str "something" (when cond1 "option 1") (when cond2 "option 2") "end")
if you have some previous programming experience (like you seem to have) I highly recommend https://www.amazon.com/Joy-Clojure-Michael-Fogus/dp/1617291412/
it covers pretty much everything, except the more recent additions to the core like transducer
great . Thanks for the recommendation quick question though, does it cover tooling and java side of clojure ?
Also omg!! lein is really easy to setup. 💯 on user on boarding after coming from js, having a single tool is such a relief
haha yeah 😄 there are a couple of different build tools in Clojure as well (Leiningen, Boot, clj), but for beginners I recommend sticking to Leiningen since it is by far the most used and because of this you can find a solution to most common issues on SO
@U7W7M34UQ, there is a #quil channel. it's really quiet down there right now. maybe I'll make some noise.
If you want to have a go with a server/client environment to apply the theory you will be learning, you can def try ... https://github.com/juxt/edge
thanks i'll keep an eye on it. but currently looking for learning the language features more
What would be a good book/resource to start learning ClojureScript? http://braveclojure.com was a good resource to learn about Clojure. Now I am at the latter part of the books, I am looking for a good resource to learn ClojureScript.
@asanka.abeyweera you really want to get familiar with libraries like reagent, hiccup, and reframe .. once you know those, you can potentially build anything.. 👌:skin-tone-3:
hi. I'm trying to solve problem #22 on 4clojure. I know this code is stupid and not optimal, but it works in my REPL. However 4Clojure complains on bad count. Can anyone please explain why?
np, btw the reason yours failed is because the underlying code in
count. I imagine the restriction check sees it too. https://github.com/clojure/clojure/blob/clojure-1.9.0/src/clj/clojure/core.clj#L4590
well, with all respect, you can't know what I'm thinking. my question was why the code behaves differently.
for is returning a sequence of N 1's, where N is the number of iterations through the for, which is one for each element in
(seq s) (I think)
So yeah, I can't see why 4clojure complains that it is giving the wrong answer.
so if you can re-formulate that to not use for (which is pretty straightforward) it will work
slightly more obscure solution:
(fn [coll] (last (map (fn [_ x] x) coll (rest (range)))))
note that collections often define their own version of count that’s optimized for their type, so in production code no, but 4clojure yes
why would you pass the init value if it is identical to the 0-arg call of f? I don't see the benefit of being extra verbose in that case
passing in a 0 value at the beginning helps you write the function being reduced as a proper monoid sum
ah, I didn't realize the 0-arg was only called given an empty collection. I thought it was always called if no initial value was given. Makes sense!
for some newer variants of reduce I believe it is always called if there is no init
Yeah, transduce always calls (f) to get an init. Contrast that to what happens when reduce has no init (from the docstring).
I've found this very error prone, so to avoid thinking about it it's better to just always supply the init.
If val is not supplied, returns the result of applying f to the first 2 items in coll, then applying f to that result and the 3rd item, etc. If coll contains no items, f must accept no arguments as well, and reduce returns the result of calling f with no arguments. If coll has only 1 item, it is returned and f is not called.
from the other side of things, there are some clojure interfaces that allow for collections with a specialized version of reduce, or defined entirely in terms of reduce, and those interfaces (clojure.langReduce and clojure.lang.ReduceInit) are distinct and are different only on if you pass in the initial value or not, and usually implementing ReduceInit (where you pass in the initial value) is easier then implementing Reduce
again, #23 on 4Clojure - why not just
into ()? instead of, for example,
(fn [coll] (reduce conj '() coll))
Very important question (:thinking_face:) here - need to know why I figure the doc for clojure.string/join and the implementation seems to have the parameters switched. Am I right or is there some hidden greatness?
str/join is multi arity, if you pass it one arg, its only a collection, if you pass it two, you pass both a seperator and a collection
that's what i thought. but both source and doc show separator and then coll. (or just coll)
i had a question about something similar. the metadata can be modified to clarify the docstring beyond what the "source" docstring actually is
noob.core> (clojure.string/join [1 2 3 4] ":") ":" noob.core> (clojure.string/join ":" [1 2 3 4]) "1:2:3:4"
in the first one it uses
[1 2 3 4] as the separator, but since you have only one character, it’s return as it is
Ah. Hum. So the doc describes two different arity implementations? (It took a while to sink in 😋)
Being on guard about undefined things silently working is new for me. Silently failing is one thing, but... hmm. Very philosophical experience, getting into Clojure.
Right, thanks. Had to double check the history, you already explained. Thanks for the patience!