An Interactive Quine in Clojure
blog.klipse.tech
blog.klipse.tech
If you learn how to write a quine in an 'ordinary' language, then the same technique works in Clojure or anything else.
SPOILER
There is basically a pattern to how it works. The program consists of a Start and a Finish section. In the middle are two arrays of strings. The first array is the strings of the code of the Start section, and the second array is the Finish section.
The Start and/or Finish section are the mechanics that prints out the two arrays of strings.
First print out the first array, eg, the Start section.
Next print out the first array again, but 'quoted' in a way that it actually forms the code that initializes the first array.
Next print out the second array in the same quoted way that it actually forms the code initializing the second array.
Last, print out the second array.
For example, the way you call a method is by using the (.myMethod args) syntax. Imagine we have something like this:
(defn myCoolFunction [myarg]
(.myMethod myarg "some argument"))
This effectively gives us a level of structured typing...I don't particularly care what myarg is, as long as it has myMethod defined.There are other features that make interop with Java pretty pleasant. The `doto` macro allows you to have a nice encapsulated system when you have to do a bunch of property setting methods.
(doto (MyClass.)
(.setSomething 1)
(.setSomethingElse 2)
(.setSomethingElse 3))
Or, for that matter, the ability to directly compose methods and functions together without intermediate variables with the threading macros (-> myObject
(.myMethod 1)
(.myOtherMethod 2)
(some-regular-function 3)
(.anotherMethod 4))
My point is not to be a salesman for Clojure (even though I love the language), but rather to point out that the interop with Java tends to work very well, at least in my experience.Curiously, lots of wrappers are 1-4 years old and still work out of the box, which is a contrast with some other packages from other ecosystems (ahem node/js)
Clojure tames Java and Javascript to the point that you don't even realize that you're working with them.