1
votes

Take a function which inputs a set, set of sets, collection, nested vector with collections buried within, etc. So, the inputs sometimes are not of the same type. How would one go about converting such input into a nested vector structure? In the first case of just a set, I would use

(into [] #{1 2 3 4})

converting the into into a vector. But the case of sets within sets, I am unable to output nested vectors, i.e.

#{1 2 3 #{1 2 3}}

Similarly, I might have an input such as

[1 2 3 (4 5 6)]

and I want to output

[1 2 3 [4 5 6]]

The idea is that sometimes I need to go within the depth and pick out a collection or set to turn into a vector. Is it possible to have a function, which in general can handle the many different structural inputs and output a nested vector structure. Namely can a function generalize the aforementioned examples? I simplified the samples somewhat, for instance I might have inputs such as

[[[1 2 3 4] [#{1 2 3 4} 2 3 4] [(1 2 3 4) 2 3 4]]]

To give more insight into the function I am trying to work on consider the function C from the language R. The importance of this function lies in the importance of vector structures within statistics/data analysis.

2
Might be able to help if you write out your input and desired output in R syntax.BrodieG
What you're probably looking for are called 'lists' in R. Lists are unstructured and allow you to store data in any nested fashion that you like. Initialize a list with the 'list()' function, and then store things using the list_variable[[name]]<-... syntax. On retrieval, the 'unlist' function will convert the contents to a vector.Dinre
I am looking to write the function in Clojure, but the premise stems from the function C in R.sunspots

2 Answers

1
votes
user=> (use 'clojure.walk)
user=> (clojure.walk/postwalk (fn [e] (if (coll? e) (vec e) e)) '(1 2 3 #{4 5 6 (7 8 9)}))
[1 2 3 [4 5 6 [7 8 9]]]
1
votes

a naive reduce implementation:

(defn to-vectors [c]
  (reduce #(conj %1 (if (coll? %2) (to-vectors %2) %2))
          [] c))

user=> (to-vectors '[[[1 2 3 4] [#{1 2 3 4} 2 3 4] [(1 2 3 4) 2 3 4]]])  
[[[1 2 3 4] [[1 2 3 4] 2 3 4] [[1 2 3 4] 2 3 4]]]