我使用atom?错了还是有别的东西....? [英] Am I using atom? wrong or there is something else....?
问题描述
基本上...
=> (atom?5)
CompilerException java.lang.RuntimeException:无法解析symbol:atom?在这种情况下,编译:(NO_SOURCE_PATH:1)
=& (atom?/ a)
RuntimeException无效的令牌:/ a clojure.lang.Util.runtimeException(Util。 java:156)
RuntimeException未匹配的分隔符:)clojure.lang.Util.runtimeException(Util.java:156)
code> => (原子?hello world)
=> (atom? "hello world")
CompilerException java.lang.RuntimeException:无法解析symbol:atom?在这种情况下,编译:(NO_SOURCE_PATH:1)
我使用Eclipse Juno 4.2,CounterClockwise插件。
So does anyone know what's happening?? I am using Eclipse Juno 4.2, the CounterClockwise plugin.
推荐答案
Clojure中的原子与其他Lisps中的原子完全不同。在经典Lisp中,原子是单个值,定义为不为null或不是cons单元格(对):
What's called an atom in Clojure is something completely different than what's called an atom in other Lisps. In classic Lisp an atom is a single value, defined as being not null or not a cons cell (pair):
(define (atom? x)
(not (or (pair? x)
(null? x ))))
在Clojure中,一个atom是一个并发引用类型。原子在Clojure可以是单值或集合/序列,其中更新(可变状态更改)保证发生原子。
In Clojure an atom is a concurrency reference type. Atoms in Clojure can be either single-valued or collections/sequences, where updating (mutable state change) is guaranteed to happen atomically.
在Clojure有更多的引用类型Lisp中的cons列表,以及所有要考虑的Java互操作集合类型。这使得很难定义对单值的检查。
In Clojure there's far more reference types than the cons list in Lisp, and there's all the Java interop collection types to be reckoned with. That makes it hard to define a check on single-values.
如果你确实想要,最简单的检查是看看是否可以计数。查看(来源计数)
,它引用 clojure.lang.RT / count和countFrom。在这里,指定了几个类/接口,我包含在以下函数中:
If you do want to, the simplest check is to see if something can be counted. Looking at (source counted)
, it references clojure.lang.RT/count and countFrom. There, several classes / interfaces are specified, which I included in the following function:
=> (defn single-valued?
[x]
(not (or (nil? x)
(.. x getClass isArray)
(some #(instance? % x) [clojure.lang.Counted
clojure.lang.IPersistentCollection
java.util.Collection
java.util.Map]))))
=> (map single-valued? [1 "foo" \a 'x true not nil])
(true true true true true true false)
=> (map single-valued? ['(1 2 3 4)
[1 2 3 4]
{:a 1 :b 2}
#{1 2 3 4}
(seq [1 2 3 4])
(seq {:a 1 :b 2})
(seq "foo")
(int-array [1 2 3 4])
(seq [])])
(false false false false false false false false false)
由于(seq [])
计算为 nil
,因此不会被视为单值。当然,具有多个字段的Java对象,以及Clojure deftypes / defrecords也会这样注册,即使它们是复合对象。
Since (seq [])
evaluates to nil
it's not considered single-valued. Of course, java objects with multiple fields, as well as Clojure deftypes / defrecords will register as such, even though they're composite objects.
这篇关于我使用atom?错了还是有别的东西....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!