有没有办法读取clojure文件中的所有表单? [英] is there a way to read all the forms in a clojure file?

查看:109
本文介绍了有没有办法读取clojure文件中的所有表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用

(-> "<file>.clj" 
    (slurp) 
    (read-string))

这将只读取文件中的第一个表单ns声明)。有没有办法从文件中检索表单列表?

That will only read the first form in the file (typically the ns declaration). Is there a way to retrieve a list of forms from the file?

如果没有使用外部库,我更喜欢。

I'd prefer if no external libraries are used.

推荐答案

[] 以使整个文件成为单个向量形式:

stick a [ and ] onto the start and end of the string to make the whole file into a single vector form:

user> (clojure.pprint/pprint 
         (read-string (str "[" (slurp "/home/arthur/hello/project.clj") "]")))
[(defproject
  hello
  "0.1.0-SNAPSHOT"
  :description
  "FIXME: write description"
  :url
  "http://example.com/FIXME"
  :license
 {:name "Eclipse Public License",
  :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies
  [[org.clojure/clojure "1.5.1"] [clj-time "0.6.0"]]
  :source-paths
  ["dev"])] 
nil

```

这篇关于有没有办法读取clojure文件中的所有表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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