Clojure-是否可以在剂量语句中增加变量? [英] Clojure - Is it possible to increment a variable within a doseq statement?
本文介绍了Clojure-是否可以在剂量语句中增加变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图遍历给定目录中的文件列表,并在它们的名称中添加一个递增变量i = {1,2,3 .....}。
I am trying to iterate over a list of files in a given directory, and add an incrementing variable i = {1,2,3.....} to their names.
这是我遍历文件并更改每个文件名的代码:
Here is the code I have for iterating through the files and changing each file's name:
(defn addCounterToExtIn [d]
(def i 0)
(doseq [f (.listFiles (file d)) ] ; make a sequence of all files in d
(if (and (not (.isDirectory f)) ; if file is not a directry and
(= '(\. \i \n) (take-last 3 (.getName f))) ) ; if it ends with .in
(fs/rename f (str d '/ i (.getName f)))))) ; add i to start of its name
我不知道如何增加 i
,因为 doseq
遍历每个文件。或者,是否有更好的循环可用来获得所需的结果?
I don't know how can I increment i
as doseq
iterates through each file. Alternatively, is there a better loop to use to achieve the desired result?
推荐答案
使用 文件序列
和 map-indexed
:
(require '[clojure.java.io :as io])
(dorun
(->>
(file-seq (io/file "/home/eduard/Downloads"))
(filter #(re-find #".+\.pdf$" (.getName %)))
(map-indexed (fn [i v] [i v]))))
在 map-indexed
中更改功能以重命名就可以了。
pdf文件的示例输出:
Change function in map-indexed
to rename and you're done.
The sample output for pdf files:
([0 #<File /home/eduard/Downloads/some.pdf>] ...)
这篇关于Clojure-是否可以在剂量语句中增加变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文