Am I using an atom? wrong or is there something else ....?

Primarily...

=> (atom? 5)

CompilerException java.lang.RuntimeException: Unable to resolve symbol: atom? in this context, compiling:(NO_SOURCE_PATH:1)

=> (atom? /a)

RuntimeException Invalid token: /a clojure.lang.Util.runtimeException (Util.java:156) RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:156)

=> (atom? "hello world")

CompilerException java.lang.RuntimeException: Unable to resolve symbol: atom? in this context, compiling:(NO_SOURCE_PATH:1)

So does anyone know what is going on? I am using Eclipse Juno 4.2, the CounterClockwise plugin.

+6
source share
5 answers

What is called an atom in Clojure is something completely different than what is called an atom in other Lisps. In classic Lisp, an atom represents a single value, defined as non-zero or non-cons cell (pair):

 (define (atom? x) (not (or (pair? x) (null? x )))) 

In Clojure, an atom is a reference type of concurrency. Atoms in Clojure can be either single-valued or collections / sequences where updating (a changed state change) is guaranteed to happen atomically.

Clojure has many more reference types than a match list in Lisp, and there are all types of Java gateway collections to be reckoned with. This makes it difficult to define a single value check.

If you want it, the easiest check is to see if you can count something. Considering (source counted) , it refers to clojure.lang.RT / count and countFrom. There are several classes / interfaces that 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) 

Since (seq []) evaluates to nil , it is not considered unambiguous. Of course, java objects with multiple fields, as well as Clojure deftypes / defrecords, will be registered as such, even if they are composite objects.

+12
source

I suspect you are confusing clojure atom with atom in a bit of a circuit.

  • In the scheme, an atom is a fundamental unit.
  • In clojure, an atom is one of the types of clojure references (e.g. ref and var) that can be updated atomically.

This goes well with the clojure concurrency model.

eg.

 user> (def a (atom '(1 2 3)]); create an atom with value (1 2 3) user> @a ; look up (deference) the atoms value (1 2 3) user> (swap! a (fn [v] (map inc v))) ; add 1 to each element, v is the ; old value of the atom. other threads will ; see the three values in a change atomically user> @a (2 3 4) user> (reset! a '(5 10 15)) user> @a (5 10 15) 
+6
source

atom? not a function.

you can use

 (def x (atom 5)) (instance? clojure.lang.Atom x) 
+3
source

Can you create an atom? follow these steps:

  (defn atom? [x] (not (coll? x)) ) 
+1
source

The complement function returns the opposite of any predicate passed to it as an argument, so can you make an atom? with it atom? :

 (defn atom? [x] ((complement coll?) x)) (atom? []) ;=> false (atom? ()) ;=> false (atom? {}) ;=> false (atom? 4) ;=> true 
0
source

Source: https://habr.com/ru/post/921973/


All Articles