Clojure Programming/Examples< Clojure Programming
This page is a collection of examples of clojure code. It's living list, so feel free to contribute. In order to make this as relevant as possible, please observe the following guidelines
- Create a separate page for your entry. Place it in Clojure_Programming/Examples/Your_Example_Name
- Create a one paragraph summary of your example, and place it in this page.
- Remember to link to your example
Thanks for doing your part to help the Clojure community!
- 1 Using JDBC
- 2 Norvig's Spelling Corrector
- 3 Simple REPL on a Socket
- 4 Simple GUI : Temperature Converter
- 5 Lazy Fibonacci
- 6 Talking to Excel
- 7 Creating an Applet
- 8 genclass and finalize
- 9 Invoking Java method through method name as a String
- 10 Setting a Java property through property name as a String
JDBC information can be found here. The page is still a work in progress.
Peter Norvig wrote an excellent example of a spelling corrector. You can see How to Write a Spelling Corrector for a more detailed explanation.
You can see the Clojure example here
Accessing a REPL over network is a very useful feature for many situations, from designing a web site to debugging a satellite in space. Here is an example how.
Simple example GUI application (from discussion group, updated for 20080329 release). It is based on Swing, and you can find it here
Interfacing to other programs via COM can be easily done in Java using the JACOB library. However coping with all the low-level interface when talking to an application is quite tedious. Especially since the interface is designed for C not for Java, not to mention Clojure. So we will try to improve the situation a little bit using the power of Clojure.
A detailed explanation is here.
This tutorial explains how to create a simple "Hello World!"-applet. The article also explains how you can sign your applet (if Reflection is needed).
genclass and finalizeEdit
gen-class functions provide the most general and powerful mechanism in Clojure for defining new Java classes. If you need to implement an existing interface you can probably use the simpler
proxy function instead, but if you need to define new Java instance methods you may need to use
By way of a quick introduction, here is an example of a new class named
MyClass in a package named
foo. This class has two methods:
mymethod, a standard Java
(gen-and-load-class 'foo.MyClass :methods [['mymethod [String] String] ['finalize  Void/TYPE]])
At this point the class exists and is loaded into the JVM, but isn't useful yet as the methods do not have definitions. Define them like this:
(clojure/ns foo) (defn MyClass-mymethod [this m] (str "Your arg was: " m)) (defn MyClass-finalize [this] (println "Finalizing " this))
This class can now be used like any Java class in Clojure:
(println (.mymethod (new foo.MyClass) "foo"))
That example creates a new instance, calls
mymethod, and prints the return value. The
finalize method will probably not be called yet, but an explicit call to the garbage collector may trigger it:
Invoking Java method through method name as a StringEdit
See Google Groups original thread for a more conversational approach.
Credit goes to Timothy Pratley, pmf, and Michael Wood for helping figure this out.
user=> (defn str-invoke [instance method-str & args] (clojure.lang.Reflector/invokeInstanceMethod instance method-str (to-array args))) #'user/str-invoke user=> (def i "sampleString")(def m "substring")(def args [2,3]) #'user/i #'user/m #'user/args user=> (apply str-invoke i m args) "m" user=>
Setting a Java property through property name as a StringEdit
(defn str-set [instance field-str arg] (clojure.lang.Reflector/setInstanceField instance field-str arg))