Clojure 白炽灯-如何使用带有列关键字的过滤器而不是N?
显然,这是因为过滤器的anon函数正在查看一个LazySeq,它的结构中不再包含列名,因此上面的代码甚至不会编译。我的问题是:白炽灯是否有办法执行这个过滤查询,仍然允许我使用列关键字?例如,我可以让它工作,因为我知道:Volume是第5列Clojure 白炽灯-如何使用带有列关键字的过滤器而不是N?,clojure,incanter,Clojure,Incanter,显然,这是因为过滤器的anon函数正在查看一个LazySeq,它的结构中不再包含列名,因此上面的代码甚至不会编译。我的问题是:白炽灯是否有办法执行这个过滤查询,仍然允许我使用列关键字?例如,我可以让它工作,因为我知道:Volume是第5列 (require '[incanter.core :as icore]) ;; Assume dataset "data" is already loaded by incanter.core/read-dataset ;; Let's examine t
(require '[incanter.core :as icore])
;; Assume dataset "data" is already loaded by incanter.core/read-dataset
;; Let's examine the columns (note that Volume is the 5th column)
(icore/col-names data)
==> [:Date :Open :High :Low :Close :Volume]
;; We CAN use the :Volume keyword to look at just that column
(icore/sel data :cols Volume)
==> (11886469 9367474 12847099 9938230 11446219 12298336 15985045...)
;; But we CANNOT use the :Volume keyword with filters
;; (well, not without looking up the position in col-names first...)
(icore/sel data :filter #(> (#{:Volume} %) 1000000))
不过,我还是想看看Incater是否有办法为这种过滤查询保留column关键字。示例数据集:
(icore/sel data :filter #(> (nth % 5) 1000000))
带有结果的查询示例:
(def data
(icore/dataset
[:foo :bar :baz :quux]
[[0 0 0 0]
[1 1 1 1]
[2 2 2 2]]))
其实这也可以写出来
(icore/$where {:baz {:fn #(> % 1)}} data)
| :foo | :bar | :baz | :quux |
|------+------+------+-------|
| 2 | 2 | 2 | 2 |
除了支持:gt
::lt
,:lte
,:gte
,:eq
(对应于Clojure的=
),:ne
(非=
),:in
,,:nin
(非in)
:fn
是通用的“使用任何函数”关键字
所有这些都可以在前面加上$
(:$fn
等),意思不变。示例数据集:
(icore/sel data :filter #(> (nth % 5) 1000000))
带有结果的查询示例:
(def data
(icore/dataset
[:foo :bar :baz :quux]
[[0 0 0 0]
[1 1 1 1]
[2 2 2 2]]))
其实这也可以写出来
(icore/$where {:baz {:fn #(> % 1)}} data)
| :foo | :bar | :baz | :quux |
|------+------+------+-------|
| 2 | 2 | 2 | 2 |
除了支持:gt
::lt
,:lte
,:gte
,:eq
(对应于Clojure的=
),:ne
(非=
),:in
,,:nin
(非in)
:fn
是通用的“使用任何函数”关键字
所有这些都可以在前面加上$
(:$fn
等)而不改变其含义