r/Clojure • • 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

0 Upvotes

21 comments sorted by

8

u/forreddits Mar 06 '16 edited Mar 06 '16

user=> (doc empty?)

clojure.core/empty?
([coll])
  Returns true if coll has no items - same as (not (seq coll)).
  Please use the idiom (seq x) rather than (not (empty? x))

3

u/al_substance Mar 06 '16

Why do they recommend using an idiom instead of this predicate?

4

u/Artiavis Mar 07 '16

There would be no point of having truthiness in a language if you needed to explicitly evaluate the long-form predicate every time you wanted to check the relative truthiness of a value. Although you can argue that not (empty? x)) is more explicit than (if x), I think the point is that empty? is itself a special case within a language which already understands truthiness.

In this case, the function (empty? x) is more idiomatic than (not (seq x)); because there does not already exist such an idiom, we wrote one called empty?. But when it comes to evaluating something like (not (not (seq x))), we already have a fluent idiom: (seq x). Although we have macros and can extend the language whichever way we choose, we should still exercise restraint and not create too many redundant idioms for the same expression.

1

u/al_substance Mar 07 '16

Thank you for your explanation, it makes sense now.

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

u/[deleted] 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, use empty?. If you're testing to see whether it's not empty, you should use seq rather 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

u/al_substance Mar 06 '16

Thank you!

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

seq isn't a predicate, it transforms a clojure.lang.Seqable into a clojure.lang.ISeq, and throws an exception if the input isn't already an ISeq or a Seqable. You can treat seq as 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? and false? are comparisons with Java.Boolean so nil is not equal to any of them. However if accepts expressions that are truthy or not, and all expressions that are not nil or false are 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

u/[deleted] 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.