'concat'打破了'line-seq'的懒惰吗? [英] Does 'concat' break the laziness of 'line-seq'?

查看:133
本文介绍了'concat'打破了'line-seq'的懒惰吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码似乎强制 line-seq 文件中读取4行。这是一种缓冲机制吗?我需要在这里使用 lazy-cat 吗?如果是这样,我如何将宏应用到序列,就像它是可变参数一样?

The following code appears to force line-seq to read 4 lines from file. Is this some kind of buffering mechanism? Do I need to use lazy-cat here? If so, how can I apply a macro to a sequence as if it were variadic arguments?

(defn char-seq [rdr]
  (let [coll (line-seq rdr)]
    (apply concat (map (fn [x] (println \,) x) coll))))

(def tmp (char-seq (clojure.contrib.io/reader file)))

;,
;,
;,
;,
#'user/tmp


推荐答案

你看到的部分是由于 apply ,因为它需要实现函数定义所需的多个参数。例如:

Part of what you're seeing is due to apply, since it will need to realize as many args as needed by the function definition. E.g.:


user=> (defn foo [& args] nil)
#'user/foo
user=> (def bar (apply foo (iterate #(let [i (inc %)] (println i) i) 0)))
1
#'user/bar
user=> (defn foo [x & args] nil)
#'user/foo
user=> (def bar (apply foo (iterate #(let [i (inc %)] (println i) i) 0)))
1
2
#'user/bar
user=> (defn foo [x y & args] nil)
#'user/foo
user=> (def bar (apply foo (iterate #(let [i (inc %)] (println i) i) 0)))
1
2
3
#'user/bar

这篇关于'concat'打破了'line-seq'的懒惰吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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