Here's an example.
user=> (if (seq []) (println 1))
nil
user=> (if (seq [1]) (println 1))
1
nil
11.05.2013, 18:40, "Kelker Ryan"
> (seq coll) will return a true value if the collection isn't empty. It will also return nil (false) if it is.
>
> 11.05.2013, 17:37, "Nico Balestra" <nicoba...@gmail.com>:
>
>> I'm not sure this question has been asked already, but I really want to know the "principle" behind (not (empty? coll)) not being idiomatic.
>>
>> I find it much more readable than (seq coll) and I don't understand why (empty?) exists if it's not idiomatic. But my real doubt is:
>>
>> What's the "idiom" in (seq coll)?
>>
>> Thanks and sorry if the question sounds a bit pedantic :)
>>
>> Nico
>>
>> "It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures" - A.J. Perlis
>>
Most of the code I see and write at work at Runa uses (not (empty? foo)). I'll continue to defend the position that it is more obvious code, and therefore better (imo :) )Alex
you could just write [...]
Most of the code I see and write at work at Runa uses (not (empty? foo)). I'll continue to defend the position that it is more obvious code, and therefore better (imo :) )
A good implementation of ISeq won't return a new object, and new short-lived objects are cheap on the JVM anyway. OTOH count can be slow for some data structures. if you don't like instantiating lots of new objects only to throw them away, you're missing out on one of HotSpot's most significant performance/convenience sweet spots, so it's strange you call that premature optimization.
There is however an optimization story against seq: Hotspot (at least on my machine) refuses to inline the call to seq by default (it might still inline if the stars are right or something, the JVM is mysterious). My guess is it's because the code in seq and seqFrom is too big, on my box it exceeds the inline threshold by one byte...
seq belongs to seq-land. empty? belongs to data structure land. It should actually be implemented as #(zero? (count %)). But unfortunately it is not.
2013/5/13 Meikel Brandmeyer (kotarak) <m...@kotka.de>seq belongs to seq-land. empty? belongs to data structure land. It should actually be implemented as #(zero? (count %)). But unfortunately it is not.I'd argue it shouldn't(empty? (cycle [1 2 3])) => false(zero? (count (cycle [1 2 3]))) ... infinite loop
That's a fair point, but do you always know that what you've gotten back is a sequence or a data structure, if you aren't looking directly at the code that you're calling?
So you think (count (map inc [1 2 3])) should be illegal?
(I'm just trying to understand your logic here)
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/WEoAo6BcCV0/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
So the following is not encouraged because we disregard the result of calling seq?
Let's not forget that allocations on the JVM are super, super cheap. In the order of something like 10 cycles. So don't worry so much about throw-away objects.
--
You misunderstood my argument. cycle returns a sequence => use seq. count is the wrong thing to call here. And calling seq without using its return value (with a name) is a smell. count should not be called in sequences. (In fact I believe that count should be O(1).)
Why shouldn't that be the case? A seq is a collection of data. And it's immutable, why would it bein its own category of collections?
Timothy
Hello,I just wanted to share the following piece from The Joy of Clojure, Chapter 3, page 45, since I found it interesting and it might be valuable to the thread:3.2Nil pun with careBecause empty collections act like true in Boolean contexts, we need an idiom fortesting whether there’s anything in a collection to process. Thankfully, Clojure provides just such a technique:(seq [1 2 3]);=> (1 2 3)(seq []);=> nilThe seq function returns a sequence view of a collection, or nil if the collection isempty. In a language like Common Lisp, an empty list acts as a false value and can beused as a pun (a term with the same behavior) for such in determining a looping termination. As you saw in section 2.3, Clojure’s empty sequences are instead truthy, andtherefore to use one as a pun for falsity will lead to heartache and despair. One solution that might come to mind is to use empty? in the test, leading to the awkwardphrase (when-not (empty? s) ...). Though it would work, this isn’t idiomatic. Abetter solution would be to use seq as a termination condition, as in the followingfunction print-seq:(defn print-seq [s](when (seq s)(prn (first s))(recur (rest s))))(print-seq [1 2]); 1; 2;=> nil(print-seq []);=> nilThere are a number of points to take away from this example. First, the use of seq as aterminating condition is the idiomatic way to test whether a sequence is empty. If wetested just s instead of (seq s), then the terminating condition wouldn’t occur evenfor empty collections, leading to an infinite loop.Regards,Alexander
There is no one who understands `(if (seq thing)` who wouldn't understand
`(if (not (empty? thing))` or, better, `(if (not-empty? thing)`. The converse is not true. That suggests that the latter should be the idiom
But, for the rest of us,