Building, Running, and The REPL | Clojure for the Brave and True

Clojure for the Brave and True
( a Book for Beginners )
Spending your nights pining for the next chapter of Clojure for the Brave and True? Then follow
@nonrecursive or get email updates: email address
Get Updates
Buy the DRM-free PDF, epub, and mobi!

Building, Running, and The REPL
In this chapter you'll invest a small amount of time up front to get familiar with a quick, problem-free way to build
and run Clojure programs. There's something powerful and motivating about getting a real program running.
Once you can do that, you're free to experiment and you can actually share your work!
You'll also learn how to instantly try out code within a running Clojure process using a REPL. This will allow you
to quickly test your understanding of the language, allowing you to learn more efficiently.
In order to get there, I'll first briefly introduce Clojure. I think you'll agree that that's a useful topic in a Clojure
book! Next, I'll cover Leiningen, the de-facto standard build tool for Clojure. By the end, you'll know how to do
the following:
Create a new Clojure project with Leiningen
Build the project to create an executable jar file
Execute the jar file
Execute code in a Clojure REPL

1. First things first: What is Clojure?
Clojure was forged in a mythic volcano by Rich Hickey. Using an alloy of lisp, functional programming, and a
lock of his own epic hair, he crafted a language which has proven to be delightful yet powerful. Its lisp heritage
gives you the power to write code more expressively than is possible in most non-lisp languages. Its distinct take
on functional programming will sharpen your thinking as a programmer and give you better tools for tackling
complex domains – like concurrent programming – which are traditionally known to drive developers into years
of therapy. All of which you'll learn about in this book.
Clojure continues to evolve. As of this writing, it's at version 1.6.alpha3 and development is going strong.
When talking about Clojure, it's important to keep in mind that Clojure is actually two things! They are:
Clojure the Language
Lisp dialect with functional emphasis
Great support for concurrency and asynchronous programming
Path to enlightenment, etc.
Clojure the Compiler
An executable JAR, clojure.jar
Takes code written in Clojure the Language and compiles it to Java Virtual Machine (JVM)


install Leiningen using the instructions from the Leiningen home page. JAR files contain Java bytecode.clj | test | | clojure_noob | | | core_test. when executed. 2. let's actually build a freakin' Clojure program! 2.md | src | | clojure_noob | | | core.clj http://www. a bazillion others Clojure is a hosted language. Running.clj | resources | README.com/getting-started/ 2/10 . Python. reads Clojure source code and produces Java bytecode. Creating a new Clojure Project Let's create our first Clojure project: lein new app clojure-noob This should create a directory structure that looks like this: | . You can read a full description of Leiningen. but for now we only care about using it for four things: 1. There is a Java program. but for now the main thing you need to understand is this: The JVM executes Java bytecode. 3.md | LICENSE | project. 2.1. C. You can refer to JAR files as Java programs. We'll explore the relationship between Clojure and the JVM more later on. and The REPL | Clojure for the Brave and True Source of confusion This distinction is necessary because. Leiningen Leiningen is a Clojure program which has become the de-facto standard build tool for Clojure.Ruby. That Java bytecode then gets executed by the JVM process which is already running clojure. 4. unlike most programming languages .10/11/2014 Building.gitignore | doc | | intro. For example. Clojure programs are executed within a Java Virtual Machine and rely on the JVM for core features like threading and garbage collection.braveclojure.jar. clojure. Now that we've got our heads straight regarding what Clojure is. which.jar. you can view the Java programming language as a DSL for emitting Java bytecode. Creating a new Clojure project Running the Clojure project Building the Clojure project Using the REPL Before going further.

resourcesis a place for you to store assets like images. in your terminal..cljin your favorite editor. make sure you're in the clojure-noobdirectory: cd clojure-noob From here.braveclojure. 2.core (:gen-class)) (defn -main "I don't do a whole lot .. 2. what function should get executed first?" src/clojure_noob/core. It's just a convention used by Leiningen. First. open src/clojure_noob/core. In general.10/11/2014 Building. little teapot! As you go through the book you'll learn what's actually happening here. yet." [& args] (println "Hello." [& args] (println "I'm a little teapot!")) Now. yet. Here's the function of each part of the skeleton: project.core (:gen-class)) (defn -main "I don't do a whole lot . Running. and The REPL | Clojure for the Brave and True There's nothing inherently special or Clojure-y about this project skeleton. It helps Leiningen answer questions like.3.cljis where we'll be doing our Clojure coding for awhile..2. "What dependencies does this project have?" and "When this Clojure program runs. "I'm a little teapot!" Congratulations. and Leiningen expects your app to be laid out this way.com/getting-started/ 3/10 . World!")) Change line 7 so that it says this: (ns clojure-noob. we won't be using it for awhile. You should see this: (ns clojure-noob. -main.cljis a configuration file for Leiningen. and that function gets run when you execute lein runat the command line.. Running the Clojure project Now let's actually run the project. Building the Clojure Project http://www. You can modify -mainand execute lein runto experiment with Clojure. you can run your Clojure program: lein run You should see the output. your source code will fall under src/{project_name} The testdirectory obviously contains tests. You'll be using Leiningen to build and run Clojure apps. but for now all you need to know is that you created a function.

prints the result. Run this: lein uberjar java -jar target/uberjar/clojure-noob-0. run. The REPL is considered an essential tool for lisp development.jaris your new.1. Before we move on to the next chapter on the Wonder and Glory of Emacs. but you might recognize that namespace from your src/clojure_noob/core.core=> The last line.10/11/2014 Building.1.braveclojure.0-SNAPSHOT-standalone. and I strongly recommend you make use of it. presenting you with a prompt again.10 Clojure 1. Try executing your -mainfunction: http://www. clojure-noob. awardwinning Clojure program which you can distribute and run on almost any platform! Awesome! We won't go into the details of how exactly Leiningen works but if you're interested in the lower-level details of compiling and running a Clojure program.org: [clojuredocs or cdoc] (user/clojuredocs name-here) (user/clojuredocs "ns-here" "name-here") clojure-noob. run this: lein repl You should see output that looks like this: nREPL server started on port 28925 REPL-y 0.4.jar Look at that! target/uberjar/clojure-noob-0.1. you can check out my article series on the topic. tells you that you're in the clojure-noob. though.corenamespace. you have all the basic details you need to build. For now. let's go over one last important tool: the REPL.core=>.clj file. To start a REPL. and distribute (very) basic Clojure programs. It then reads your input. It will let you quickly check your understanding of the language and explore ideas.5. and The REPL | Clojure for the Brave and True Now let's create a JAR file which you can distribute for big $$$.0-SNAPSHOT-standalone. We won't get into namespaces now. It presents you with a prompt and you type code into it. Running.1 Exit: Control+D or (exit) or (quit) Commands: (user/help) Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) (user/sourcery function-name-here) Javadoc: (javadoc java-object-or-class-here) Examples from clojuredocs. 2.com/getting-started/ 4/10 . evaluates it. and loops. Using the REPL REPL stands for "Read-Eval-Print Loop" and it's a tool for experimenting with code.

At this point you should have the basic knowledge you need to begin learning the Clojure language without having to fuss with tools. the Clojure REPL allows you to monkey around with a running Clojure process. and The REPL | Clojure for the Brave and True clojure-noob.core=> (-main) I'm a little tea pot! nil Cool! Try a few more basic Clojure functions: clojure-noob. little teapot.10/11/2014 Building. a plugin for IntelliJ 3. Chapter Summary I'm so proud of you. Amazing! It brings to mind the immortal lines from Taylor Swift's master songwriting: You held your head like a hero On a history book page It was the end of a decade But the start of an age . This can be very powerful. To learn how to interact with Clojure with even more proficiency. If Emacs isn't your cup of tea. we'll cover Emacs in depth. the REPL is similar to SSH. Running. Voice of Our Age Bravo! ← Introduction Start Using Emacs → 12 Comments  Clojure for the Brave and True http://www. as you can even attach a REPL to a live.core=> (+ 1 2 3 4) 10 clojure-noob. though. here are some resources for setting up other text editors for Clojure development: Clojure development with Sublime Text 2 (youtube) Writing Clojure with Vim in 2013 Counterclockwise is a highly-recommended Eclipse plugin Getting Started with La Clojure. In the same way that you can use SSH to interact with a remote server.core=> (* 1 2 3 4) 24 clojure-noob. we'll be using the REPL to build our knowledge of Clojure syntax and semantics.core=> (first [1 2 3 4]) 1 Awesome! Conceptually. For now. production app and modify your program as it runs.braveclojure. one of the most important tools for developing Clojure software. you've become acquainted with the REPL.com/getting-started/ Share Login 5/10 . however. You've run your first Clojure program! Not only that.Taylor Swift.