Emi*_*mil 3 text functional-programming clojure matching
使用这个答案的代码,我有
(defn repeat-image [n string]
(println (apply str (repeat n string))))
(defn tile-image-across [x filename]
(with-open [rdr (reader filename)]
(doseq [line (line-seq rdr)]
(repeat-image x line))))
Run Code Online (Sandbox Code Playgroud)
...水平平铺ascii图像 现在,我怎么能"忽略"第一行?我这样做的原因是每个图像都有坐标(例如"20 63")作为第一行,我不需要该行.我尝试了一些方法(保持索引,模式匹配),但我的方法感觉做作.
假设您想要跳过文件的第一行并像处理其余的那样处理剩下的行tile-image-across,您可以简单地替换(line-seq rdr)为
(next (line-seq rdr))
Run Code Online (Sandbox Code Playgroud)
实际上,您可能应该考虑选择相关的行和处理:
;; rename repeat-image to repeat-line
(defn read-image [rdr]
(next (line-seq rdr)))
(defn repeat-image! [n lines]
(doseq [line lines]
(repeat-line n line)))
Run Code Online (Sandbox Code Playgroud)
在里面使用with-open:
(with-open [rdr ...]
(repeat-image! (read-image rdr)))
Run Code Online (Sandbox Code Playgroud)
相反,如果您的文件包含多个图像,并且您需要跳过每个图像的第一行,最好的方法是编写一个函数来将行的seq分割成一系列图像(如何完成取决于格式您的文件),然后映射,超过(line-seq rdr)和(map next ...))对结果:
(->> (line-seq rdr)
;; should partition the above into a seq of seqs of lines, each
;; describing a single image:
(partition-into-individual-image-descriptions)
(map next))
Run Code Online (Sandbox Code Playgroud)
NB.懒惰的partition-into-individual-image-descriptions这将产生一个懒惰的seqs seqs; 在with-open关闭读者之前你需要消费它们.