简体   繁体   English

获取列表clojure中的元素类型

[英]Get type of elements in list clojure

I have a quoted expression eg 我有一个引用的表达,例如

(def foo '(+ 1 (bar)))

I want to be able to find the type of any element of this list, eg to do something like 我希望能够找到此列表中任何元素的类型,例如,执行类似的操作

(type (second foo))

I get different results depending on the type of value, for example the following both evaluate to java.lang.Double, which is what I would want 我得到不同的结果取决于值的类型,例如以下两者都评估为java.lang.Double,这是我想要的

(type '3.0)
(type 3.0)

Yet 然而

(type '+)
(type +)

yields respectively 收益率分别

clojure.lang.Symbol
clojure.core$_PLUS_

I thought perhaps the resolve key would help yet 我想也许解决方案的关键还有帮助

(type (resolve '+))

evaluates to: 评估为:

clojure.lang.Var clojure.lang.Var

There are just symbols '+ , 'bar and self-evaluating object 1 (long) in your list 列表中只有符号'+'bar和自我评估对象1 (长)

(= '(+ 1 (bar))
   (list '+ '1 (list 'bar)))
=> true

Use eval . 使用eval

(type (eval '+)) 
=> clojure.core$_PLUS_

And

(type '3.0) = (type 3.0) = (type (eval '3.0)) = java.lang.Double

because numbers are self-evaluating objects. 因为数字是自我评估的对象。

取消引用已解决的Var:

(type (deref (resolve '+)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM