当使用“休息”时,保持在序列的头部上。 [英] Holding onto the head of sequence when using "rest"

查看:138
本文介绍了当使用“休息”时,保持在序列的头部上。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析一个大的csv文件,我使用它的第一行作为记录的键。因此,对于csv文件,如:

I am parsing a big csv file and I am using the first line of it as the keys for the records. So for a csv file like:

header1,header2
foo,bar
zoo,zip

我最终得到了一个惰性的seq,如:

I end up with a lazy seq like:

({:header1 "foo" :header2 "bar"},
 {:header1 "zoo" :header2 "zip"})

代码工作正常,但我不知道如果在下面的函数中我持有线的头。

The code working fine, but I am not sure if in the following function I am holding the head of "lines" or not.

(defn csv-as-seq [file]
  (let [rdr (clojure.java.io/reader file)]
    (let [lines (line-seq rdr)
         headers (parse-headers (first lines))]
      (map (row-mapper headers) (rest lines)))))

有人可以澄清吗?

推荐答案

是的,这个表达式语法上说要保持头

Yes, this expression syntactically says to hold the head

(let [lines (line-seq rdr)

虽然在这种情况下你应该放弃它,因为他们没有引用调用映射后的
lines headers 和以1.2.x开头的Clojure编译器包括一个特性调用局部清除:在函数调用的前导中将函数调用后的任何局部变量设置为nil,在这种情况下,它将在函数的局部上下文中将行和头设置为nil,他们将使用GCd,这是clojure产生不能在java中表示的字节码的罕见情况之一。

though in this case you should get away with it because their are no references to lines and headers after the call to map and the Clojure compiler starting with 1.2.x includes a feature called locals clearing: it sets any locals not used after a function call to nil in the preamble to the function call. In this case it will set lines and headers to nil in the local context of the function and they will be GCd as used. This is one of the rare cases where clojure produces bytecode that cannot be expressed in java.

这篇关于当使用“休息”时,保持在序列的头部上。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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