Clojure-将字符串转换为整数向量的向量 [英] Clojure - Convert string into vector of vector of integers

查看:57
本文介绍了Clojure-将字符串转换为整数向量的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析一个文件,并使用每一行来执行任意数量的函数和参数.我要调用的函数接受两个整数向量的向量以进行矩阵乘法.我能够将参数解析为一个向量,因此可以在其上调用apply和已解析的函数符号.但是我仍然需要将参数从字符串转换为适当的类型.我该如何实现?

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?

函数头示例:

(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))))

(vec参数)的格式:

Format of (vec args):

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

我需要将args向量中的每个矩阵转换为上述整数向量的向量.Clojure新手将不胜感激!

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!

推荐答案

您可以使用 clojure.edn/read-string 将字符串解析为数据结构:

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]]]

这篇关于Clojure-将字符串转换为整数向量的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆