Search code examples
clojureclojureclr

Clojure: finding out if a collection is seq-able


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

  • a map (i.e. something that contains key-value pairs)
  • a collection (i.e. something that contains values)
  • a non collection value like a string.

Is there a better way than

#(or (seq? %) (list? %) etc)

Solution

  • 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

    • already a seq
    • an instance of clojure.lang.Seqable
    • nil
    • instance of Iterable
    • an array
    • a string
    • instance of java.util.Map