Question

I'd like to write a function that takes vectors [& x] and applies a test to pairs of elements. Outputting a vector of elements the test deemed identical and a vector of nil elements.

My first thought is to take the vectors and flatten them.

(defn soup [& x]
    (vec (flatten x))

Then apply a test such as identical? neg? or something along those lines. It's at the point of pattern matching that I am stuck in trying to assemble the output.

Ex) Input 1: [:a :b :c :a :b :c]

Output 1: [[:a :a] [:b :b] [:c :c]]

Input 2: [[:a :b :c] [:a :b :c]]

Output 2: [[[:a :b :c] [:a :b :c]]]

If Input 2 is first flattened, it gives back Output 1.

Was it helpful?

Solution

Would combining sort and partition-by be close to what you are asking for?

(->> [:a :b :c :a :b :c] sort (partition-by identity))
((:a :a) (:b :b) (:c :c)) 

(->> [[:a :b :c] [:a :b :c]] sort (partition-by identity))
(([:a :b :c] [:a :b :c]))

and if you need them to be vectors after:

(->> [:a :b :c :a :b :c] sort (partition-by identity) (map vec) vec)
[[:a :a] [:b :b] [:c :c]]

(->> [[:a :b :c] [:a :b :c]] sort (partition-by identity) (map vec) vec)
[[[:a :b :c] [:a :b :c]]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top