0
votes

I'm trying to parse through a file and use each line to execute any number of functions and parameters. The functions I want to call accept two vectors of vectors of integers for matrix multiplication. I'm able to parse the arguments into one vector so I can call apply on it and the resolved function symbol. But I still need to convert the arguments from strings into the appropriate type. How can I achieve this?

Function header example:

(defn ijk [[& matrixA] [& matrixB]]
   ...
  )

Input file example: (splitting string by commas)

ijk,[[1 2] [3 4]],[[1 2] [3 4]]
kij,[[2 2] [3 4]],[[1 2] [3 4]]

How I'm reading the file so far:

(defn get-lines [fname]
  (with-open [r (reader fname)]      
    (loop [file (line-seq r)]
      (if-let [[line & file] file]
        (do (let [[command & args] (str/split line #",")]
              ;apply (resolve (symbol command)) (vec args))    
              )
            (recur file))
        file))))

Format of (vec args):

[[[1 2] [3 4]] [[1 2] [3 4]]]
[[[2 2] [3 4]] [[1 2] [3 4]]]

I need to convert each matrix in the args vector into a vector of vectors of integers above. Any and all help is much appreciated by this Clojure noob!

1

1 Answers

3
votes

You could use clojure.edn/read-string to parse the strings into data structures:

(def args ["[[1 2] [3 4]]"
           "[[1 2] [3 4]]"])
(mapv clojure.edn/read-string args)
=> [[[1 2] [3 4]] [[1 2] [3 4]]]