I have an application which I originally implemented in Common Lisp, and recently re-implemented in Scala. I’d like to try to re-re-implement this in Clojure. This will be my first Clojure program.
Most of the parts of this program will be straightforward to implement. However, there are a few things which worry me as I don’t yet know enough about Clojure to envision an implementation. Before I start, I have some questions to see whether my strategy will work.
I plan to define a record call Bdd which has three fields: an Integer, and two Bdd objects. My application will allocate many 100s of thousands of such objects and manipulate them using an algebra which I will define.
-
Can I define a
print-objectortoStringmethod to control how the system prints a Bdd instance? (of course I don’t care that it be a method, just some way to control how my object is serialised for printing in the REPL etc) -
Can I define a multi-method which accepts two arguments? then define 4 methods for each of (integer,integer), (integer,Bdd), (Bdd,integer), (Bdd,Bdd) ? I read about multi-methods a bit, but it is not clear how to make them work for Integers and records.
-
There are exactly two distinguished Bdd objects, BddTrue and BddFalse, which do not have any fields, Normally I’d define these as an algebraic data type (which I did in Scala and in Common Lisp). What’s the approach in Clojure? In Scala and Common Lisp these distinguished BddTrue and BddFalse objects satisfy an is-a relationship. BddFalse is-a Bdd and BddTrue is-a Bdd.
-
I never want to compare two Bdd objects by their content, only by their identity. They should only be considered equal if they are the exact same object? And I will enforce in the factory function that never are to Bdd instances ever created which have the same content. Do I need to always remember to use
identical?or can I somehow tell the system what equal means on instances of this record? -
I need a weak-value hash table to store the objects, to assure that never do two get allocated with the same content. In Scala I’m using the java class
org.jboss.util.collection.WeakValueHashMap. In The CL implementation I used the SBCL weak-hash-table which just did what I wanted automatically. What’s the correct way to do this in Clojure?
Thanks to anyone who can offer some advise on some/any of these points before I begin.