Clojure Programming/Examples

From Wikibooks, open books for an open world
Jump to navigation Jump to search

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!

Using JDBC[edit | edit source]

JDBC information can be found here. The page is still a work in progress.

Norvig's Spelling Corrector[edit | edit source]

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

Simple REPL on a Socket[edit | edit source]

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 GUI : Temperature Converter[edit | edit source]

Simple example GUI application (from discussion group, updated for 20080329 release). It is based on Swing, and you can find it here

Lazy Fibonacci[edit | edit source]

Fibonacci numbers are a classic example of an infinite recursive sequence. Clojure's Lazy functions are an excellent way to implement this mathematical construct. You can see a detailed example here.

Talking to Excel[edit | edit source]

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.

Creating an Applet[edit | edit source]

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 finalize[edit | edit source]

The 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 gen-and-load-class or gen-and-save-class.

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 finalize:

(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:

(System/gc)

Invoking Java method through method name as a String[edit | edit source]

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 String[edit | edit source]

(defn str-set [instance field-str arg]
    (clojure.lang.Reflector/setInstanceField
        instance 
        field-str 
        arg))