News for hackers. Stuff that matters.
Re:Clojure Style Guide by danneu on Saturday August 09, 2014 @11:35AM (#8157092)

    ;; good
    (defn print-seq [s]
      (when (seq s)
        (prn (first s))
        (recur (rest s))))

    ;; bad
    (defn print-seq [s]
      (when-not (empty? s)
        (prn (first s))
        (recur (rest s))))
Why use `seq` instead of `not-empty`?

    (defn print-seq [s]
      (when (not-empty s)
        (prn (first s))
        (recur (rest s))))
Re:Clojure Style Guide by mullr on Saturday August 09, 2014 @11:59AM (#8157156)
(when (seq s) ...) reads to me as "do this as long as I can treat s sequentially". The following operations on s require it to be seqable, so it's like a runtime type check. not-empty is less direct, and feels like it's assuming that s is seqable.

Of course, not-empty is implemented with (when (seq)), so there's no REAL difference. At the end of the day, (when (seq)) is what people use.

Re:Clojure Style Guide by mkremins on Saturday August 09, 2014 @02:10PM (#8157544)
This interpretation is error-prone. (seq 1), for instance, throws an IllegalArgumentException at runtime. Meanwhile, (seq nil) returns a falsey value even though in many cases it's possible to transparently pass nil in place of a collection of any kind.

Essentially, seq already assumes that its argument is seqable. Don't use it to test an argument of unknown type for seqability; prefer something like the seqable? function from core.incubator [1], or a different core function like coll? or sequential? where the semantics of one of these will do.

[1] https://github.com/clojure/core.incubator/blob/master/src/ma...

Re:Clojure Style Guide by justwannareply on Saturday August 09, 2014 @12:13PM (#8157187)
I don't know any Clojure, but I guess it's for the same reason why

  if lst:
    # do something with lst
is preferred to

  if len(lst) > 0:
    # do something with lst
in Python. It becomes more obvious when you use descriptive names:

  if customers:
    # do something with customers
Re:Clojure Style Guide by tel on Saturday August 09, 2014 @12:21PM (#8157204)
... but involves such an overloading of the nature of truthiness! All to match a really ad hoc syntax sugaring of English?

    if X ==> if there exists some X
Re:Clojure Style Guide by kyllo on Saturday August 09, 2014 @04:39PM (#8157957)
In Python, "if there exists some X" would be more like

     if x is not None:
In Python, [] (empty list) and {} (empty dict) are == False but are not None. They exist, and their types are List and Dict rather than NoneType, but they are "falsey" because they are empty.
Re:Clojure Style Guide by diego on Saturday August 09, 2014 @12:29PM (#8157226)

  (not-empty coll)
is the same as

  (when (seq coll) coll)
Re:Clojure Style Guide by thom on Sunday August 10, 2014 @05:11AM (#8159279)
Yeah, it's worth noting that not-empty is the flip-side to empty (which returns an empty coll of the same type as its argument), as opposed to empty?.
Re:Clojure Style Guide by bozhidar on Saturday August 09, 2014 @01:20PM (#8157392)
There's still debate on this (https://github.com/bbatsov/clojure-style-guide/issues/44). It's the suggested style mostly because Rich (Clojure's creator) promotes it.