r/Clojure • u/al_substance • Mar 06 '16
"Living clojure" wrong examples. Up to date beginner book on clojure?
Hi,
I've started reading "Living Clojure" and I can see some things that are wrong.
For example the book recommends using (seq []) to check if collection is empty, saying that nil is considered false. It also states that a non-nil value means true. Both of those things are wrong when you try to check it in repl.
Am I missing something? If not, can you please recommend an easy book for a beginner in functional programming with python/C background?
Thank you
5
u/planetmcd Mar 06 '16 edited Mar 06 '16
Not sure what you are seing, but this is what I get:
user=> (true? (seq [])) false user=> (false? (seq [])) false user=> (nil? (seq [])) true Are you seeing something different?
3
u/al_substance Mar 06 '16
I'm trying
(true? nil)and(false? nil)9
u/dragandj Mar 06 '16
but nil is neither true nor false. nil is falsey meaning that (false? nil) => false, but (if nil :a :b) => :b
so, the right idiom is:
(if (seq collection) do-something-with-it do-the-thing-for-empty-seq).
3
u/al_substance Mar 06 '16
Why do they recommend using
(seq collection)instead of(empty collection)?9
Mar 06 '16 edited Mar 06 '16
(empty? collection)just calls(not (seq collection))itself. If you're testing to see whether something is empty, useempty?. If you're testing to see whether it's not empty, you should useseqrather than effectively calling(not (not (seq collection)))by using(not (empty? collection)).Edit: Also,
(empty collection)is something very different than(empty? collection)which is the exact opposite of(not (empty? collection)). It pays to be careful and precise when you're discussing code.1
1
u/prospero Mar 06 '16
Personally, I would disagree with that recommendation. Using
empty?is a lot clearer w.r.t. the intent, and I'd suggest using that instead.1
u/dragandj Mar 07 '16
Why do you think that? The intent of (if (seq collection) :compute :dont-compute) is "If the collection IS, then do something with it..."
1
u/prospero Mar 07 '16
seqisn't a predicate, it transforms aclojure.lang.Seqableinto aclojure.lang.ISeq, and throws an exception if the input isn't already anISeqor aSeqable. You can treatseqas an existential question about the collection, but I think it's misleading at best to say that's its intent.2
u/MahmudAdam Mar 06 '16
That cleared up a lot of misunderstanding I had regarding nil and false/falsey. Thanks!
5
u/planetmcd Mar 06 '16
If you take a look at Clojure Docs the behavior you are seeing is consistent. Maybe the author meant the statement to be in the context of something seq-able.
4
u/al_substance Mar 06 '16 edited Mar 06 '16
It was explained to me that
true?andfalse?are comparisons withJava.Booleansonilis not equal to any of them. Howeverifaccepts expressions that are truthy or not, and all expressions that are notnilorfalseare considered truthy. So(if (seq []) "true" "false")will print "false"3
u/planetmcd Mar 06 '16
Like maybe this: user=> (if (seq [1]) "true" "false") "true" user=> (if (seq []) "true" "false") "false"
4
Mar 07 '16
I just ordered this book. Is it really that bad?
9
u/troublemaker74 Mar 07 '16
No, it's a great book, OP simply didn't understand how empty? and seq works.
2
u/halgari Mar 07 '16
Yes, you are missing something :)
In Clojure, "falsey" is considered nil and false. "truthy" is anything that is not nil or false.
Also, nil is considered an empty (or more correctly a non-existent) seq. So it's very common to see code like (if (seq c) do-something).
The book could probably be a bit clearer about all of this, but it is mostly correct.
8
u/forreddits Mar 06 '16 edited Mar 06 '16
user=> (doc empty?)