Tag: Clojure

  • These are instructions to get started with Clojure using IntelliJ IDEA, La Clojure and Leiningen for people that don’t know any of those tools. They are for Mac OS X but they may be adaptable to other operating systems as well.

    It doesn’t expect you to know Clojure or IntelliJ IDEA. I think this might be a good way for beginners to get started (instead of Emacs for example). I been using RubyMine for quite a while and I’m happy with it. The only requirement in installing Homebrew on your mac, which you should anyway if you want to code.

    Install Clojure and Leiningen using Homebrew:

    [sourcecode lang=”bash”]
    brew install clojure
    brew install leiningen
    [/sourcecode]

    Download IntelliJ IDEA and install it:

    Run it:

    Open the preferences (⌘,) and go to the plugins section:

    Download and install La Clojure plugin:

    Download and install the Leiningen plugin.

     

    Restart IntelliJ IDEA:

     

    Create a Leiningen project:

    lein new foobar
     Created new project in: /Users/pupeno/Temporary/foobar

    Open the project in IntelliJ IDEA:

     

    Now open Project Structure (⌘;) and go to SDK. If you tried to run the Clojure REPL and got the error “No jdk for module ‘foobar’”, this is what you need to do to fix it:

    Click on the plus sign and add a JSDK:

    The default is just fine:

    And you should see something like:

    Go to the project

    and select the newly created 1.6 SDK:

    Go to modules

    open dependencies:

    and add a single entry:

    Use the installed Clojure from /usr/local/Cellar/clojure/1.2.1/clojure.jar:

    I’m not totally sure about that step. It might be that the IntelliJ project you are creating works only on a machine where Clojure is located on the same path.

    As they say… works for me! Restart IntelliJ… not sure if you’ll need to, but I needed it.

    Open the project if it wasn’t open and start the Clojure REPL (⇧⌘F10), it’s in the Tools menu:

    It works:

    Open a file:

    Type a little program, like:

    [sourcecode lang=”clojure”]
    (defn hello-world []
    (println "Hello world"))
    [/sourcecode]

    Load the file on the REPL (⇧⌘L), which is in Tools → Clojure REPL:

    Enjoy magnificent code completion:

    and run the code:

    And that’s it. Whether IntelliJ IDEA and La Clojure is a viable developing environment only time will tell.

  • Let’s compare how we print the class-path in Clojure and how we do it on Java.

    In Clojure:

    [sourcecode lang=”clojure”]
    (println (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))
    [/sourcecode]

    In Java:

    [sourcecode lang=”clojure”]
    import java.net.URL;
    import java.net.URLClassLoader;

    public class PrintClasspath {
    public static void main(String[] args) {
    //Get the System Classloader
    ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

    //Get the URLs
    URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();

    for(int i=0; i< urls.length; i++)
    {
    System.out.println(urls[i].getFile());
    }
    }
    }
    [/sourcecode]

    To be fair, the output is not the same, but the effect is.

  • Being able to write, build and run a Clojure application, like I explained in a previous article, is not enough. You also want to hack on it, to iterative code on it, after all, you are using a Lisp.

    What I mean by iterative coding is something not very few know or do, but it’s extremely common in Lisp. You have you REPL running all the time (that is, generally, the interpreter). You load the code into the REPL, run it, modify some part of the code and re-load it. You may not reload the whole file but only a function on it, and you may have background process running on the REPL, like a web server. It is very powerful. (more…)

  • Update: this is not my preferred way to create a Clojure application and shouldn’t be yours either, check out Leiningen

    This is one of those posts that I publish partly for myself. And partly so people can criticize my way, which is also for myself, and only incidentally for others to learn from it.

    It seems Maven is popular in the Clojure world. Clojure itself uses it, Webjure uses and its demo application uses it as well, there’s a branch for Compojure that uses too. So after building all those components using Maven I’ve decided to use it myself when building Clojure applications.
    (more…)