Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Basically...

=> (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's happening?? I am using Eclipse Juno 4.2, the CounterClockwise plugin.

like image 904
Zchpyvr Avatar asked Aug 02 '12 17:08

Zchpyvr


People also ask

Should I use Atom or Vscode?

Visual Studio Code and Atom, both Electron-based applications give a good user experience but when it comes to comparison, Visual Studio Code leaps ahead. Visual Studio Code has a greater number of built-in features than Atom provides through extensions and third-party applications.

Do people still use Atom?

GitHub Is Sunsetting Atom Text Editor "We are sunsetting Atom and will archive all projects under the organization on December 15, 2022," GitHub wrote in a blog post. As a free and open-source text editor that debuted in 2011, Atom has been one of the go-to options for plain text editing and software development.

What happened to Atom editor?

We are archiving Atom and all projects under the Atom organization for an official sunset on December 15, 2022.


2 Answers

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 ))))

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.

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.

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)

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.

like image 177
17 revs Avatar answered Nov 15 '22 09:11

17 revs


I suspect you are confusing a clojure atom with an atom in something like scheme.

  • In scheme an atom is a fundamental unit.
  • In clojure an atom is one of clojure's reference types (like ref and var) that can be updated atomically.

This fits nicely with clojure's concurrency model.

e.g.

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)
like image 22
sw1nn Avatar answered Nov 15 '22 09:11

sw1nn