concept object in category clojure

appears as: object, n object, Objects, objects, object, The object
Clojure: The Essential Reference

This is an excerpt from Manning's book Clojure: The Essential Reference.

  • "f" is a function of any number of arguments that can return an invocable object (such that (fn? object) yields true). "f" will need to return an object so that (fn? object) is false at least once to prevent trampoline from going into an infinite recursion.
  • (require '[no.disassemble :refer [disassemble]])
    (println (disassemble (fn [] (large-let 2))))
    
    public final class LetPerf extends clojure.lang.AFunction {
    
      // Omitted some static class attributes declaration.
    
      // Method descriptor #11 ()Ljava/lang/Object;
      // Stack: 6, Locals: 5
      public java.lang.Object invoke() {        #1
         // 0  lconst_0
         // 1  lstore_1 [a0]
         // 2  lconst_1
         // 3  lstore_3 [a1]
         // Omitted bytecode related to loading reduce
        // 28  lload_1 [a0]
        // 29  invokestatic clojure.lang.Numbers.num(long) : java.lang.Number [34]
        // 32  lload_3 [a1]
        // 33  invokestatic clojure.lang.Numbers.num(long) : java.lang.Number [34]
        // 36  invokeinterface clojure.lang.IFn.invoke(Object, Object) : Object [37]
        // 41  invokeinterface clojure.lang.IFn.invoke(Object, Object) : Object [37]
        // 46  areturn
      }
    
      // Omitted static block initializer
    }
    Figure 9.4. Recursive use of lazy-seq produces a concatenation of cons objects with a final LazySeq holding the unrealized rest of the infinite sequence.
    30 lazy seq linear

    bean is a function that creates a map-like representation of the attributes available in an object:

    Clojure in Action, Second Edition

    This is an excerpt from Manning's book Clojure in Action, Second Edition.

    What does all of this have to do with Clojure? All of the classes located in Java’s java.lang package are imported by default in all Clojure programs, so that you can refer to things like String and Integer without having to type out java.lang.String and java.lang.Integer. Many Clojure data structures (especially collections) implement Java interfaces, so Java libraries that expect objects that implement those interfaces will accept Clojure data structures as arguments. All Clojure collections, for example, implement java.lang.Iterable or java.util.Collection, whereas only some implement java.util.List or java.util.Map, depending on their purpose.

    Now, because this return is a lazy sequence, it no longer attempts to recursively compute all the elements to return. The lazy-seq macro takes a body of code that returns a sequence (or nil) and returns an object that’s “seqable”—that is, it behaves like a sequence. But it invokes the body only once and on demand (lazily) and returns cached results thereafter. The function now works as expected:

    That works, but it’s far from pretty. You should be able to support the same familiar interface of calling methods on objects that they belong to. Let’s expand the capability of the message-passing system you’ve built so far to handle such methods also. Here’s an updated version of new-object that does this:

    (defn new-object [klass]
      (let [state (ref {})]
        (fn [command & args]
          (case command
            :class klass
            :class-name (klass :name)
            :set! (let [[k v] args]
                    (dosync (alter state assoc k v))
                    nil)
            :get (let [[key] args]
                   (@state key))
            (if-let [method (klass :method command)]
              (apply method args)
              (throw (RuntimeException.
                       (str "Unable to respond to " command))))))))

    What’s added here is a default clause to case. If the message passed in isn’t one of :class, :class-name, :set!, or :get, then you assume it’s a method call on the object. You ask the class for the function by passing along the received command as the method name, and if you get back a function, you execute it. Here it is in action:

    First, a new function object is created by calling the new-user function with "arjun", "secret", and "arjun@currylogic.com" as arguments. Then, you were able to query it for the login, password, and email address. arjun appears to behave something like a map, or at least a data object of some kind. You can query the internal state of the object using the keywords :login, :password, and :email, as shown previously.

    The Joy of Clojure, Second Edition

    This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

    It’s important to note that in the canon of object-oriented programming (OOP), there’s no clear distinction between state and identity. In other words, these two ideas are conflated into what’s commonly referred to as mutable state. The classical object-oriented model allows unrestrained mutation of object properties without a willingness to preserve historical states. Clojure’s implementation attempts to draw a clear separation between an object’s state and identity as they relate to time. To state the difference from Clojure’s model in terms of the aforementioned flip book, the mutable state model is different, as shown in figure 1.8.

    Figure 1.8. The Mutable Runner. Modeling state change with mutation requires that you stock up on erasers. Your book becomes a single page: in order to model changes, you must physically erase and redraw the parts of the picture requiring change. Using this model, you should see that mutation destroys all notion of time, and state and identity become one.

    Immutability lies at the cornerstone of Clojure, and much of the implementation ensures that immutability is supported efficiently. By focusing on immutability, Clojure eliminates the notion of mutable state (which is an oxymoron) and instead expounds that most of what’s meant by objects are instead values. Value by definition refers to an object’s constant representative amount,[11] magnitude, or epoch. You might ask yourself: what are the implications of the value-based programming semantics of Clojure?

    The problem is that Clojure’s into-array function doesn’t return a primitive array of char[], but instead returns a reference array of Character[], forcing the Clojure compiler to resolve the call to the StringBuilder.append(Object) method instead. That the Array class is a subclass of Object is a constant cause for headache in Java and clearly can be a problem[11] for Clojure as well. What you really want to do is ensure that a primitive array is used as the argument to .append, as shown here: