So there's list?, seq?, vector?, map? and so on to determine what type of collection the argument is.
What's a good way of telling the difference between
Is there a better way than
#(or (seq? %) (list? %) etc)
Clojure defines many algorithms in terms of sequences (seqs). A seq is a logical list, and unlike most Lisps where the list is represented by a concrete, 2-slot structure, Clojure uses the ISeq interface to allow many data structures to provide access to their elements as sequences.
Sequences don't use inline functions, therefore, new Function objects are created for each operation. On the other hand, collections create a new list for every transformation while sequences just keep a reference to the transformation function.
"Collection" and "Sequence" are abstractions, not a property that can be determined from a given value. Collections are bags of values. Sequence is a data structure (subset of collection) that is expected to be accessed in a sequential (linear) manner.
Clojure collections "collect" values into compound values. There are four key Clojure collection types: vectors, lists, sets, and maps. Of those four collection types, vectors and lists are ordered.
using seq?
is about as concise and clean as it gets.
clojure.contrib.core defines:
seqable? function Usage: (seqable? x) Returns true if (seq x) will succeed, false otherwise.
http://clojure.github.com/clojure-contrib/core-api.html
it does what you proposed with one big or
statement of
Let's not forget about sequential?
:
user=> (sequential? [])
true
user=> (sequential? '())
true
user=> (sequential? {:a 1})
false
user=> (sequential? "asdf")
false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With