Search code examples
clojureclojure-contrib

Testing vectors and nested vectors in Clojure


Is there a way in Clojure to test a vector and see if it's nested, i.e. a way to test [:a :b :c :d] vs. [[:a :b] [:c :d]]?

I've tried the test

(vector? [:a :b :c :d])
 true

but it remains true for nested vectors as well,

(vector? [[:a :b] [:c :d]])
 true

Solution

  • checking if any of them are sequential seems close:

    user> (every? #(not (sequential? %)) [:a :b :c :d])
    true
    user> (every? #(not (sequential? %)) [:a :b :c :d [:e]])
    false
    

    because all the base collections can be made into sequences, though it may be necessary to also check for Java arrays:

    (every? #(not (sequential? %)) [:a :b :c :d (into-array [1 2 3])])