In the previous chapters we set up our development environment, now it’s time to to get our hands dirty.
First of all we need to have Java installed, Clojure is a hosted language, and it compiles to JVM bytecode, so, we need Java installed.
After it install Leiningen fallowing the install instructions, it is the most popular build/dependency management tool for Clojure, and we’ll use it a lot while learning Clojure.
After install, just call
lein at your terminal, and it should prompt help instructions.
Its is one of the most important thing in your Clojure development flow (and should be in every language, if its available), it will give you a interactive programming experience.
This can sound like a bullshit for everyone that’s already used to programming in one language that already have one, but from the folks coming from environments that doesn’t have it, will be beginning of a new era, an era with REPL!
Just open your terminal and type
lein repl and it will start a new REPL for you:
If you don’t want to use
lein to start our REPL, just type
clj and it will start too.
$ > lein repl nREPL server started on port 46213 on host 127.0.0.1 - nrepl://127.0.0.1:46213 REPL-y 0.4.4, nREPL 0.7.0 Clojure 1.10.1 OpenJDK 64-Bit Server VM 1.8.0_252-b09 Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) Javadoc: (javadoc java-object-or-class-here) Exit: Control+D or (exit) or (quit) Results: Stored in vars *1, *2, *3, an exception in *e user=>
Now every expression that you type will be evaluated:
user=> (+ 41 1) 42
In one future post, we’ll learn how to integrate it with our text editor and have a full interactive programming experience!
REPL is not only evaluate expressions, in the future we’ll learn how to use it for debuggind, run test and much more, but for now, we’ll use it only for evaluating expressions while learning the beginners things!
One of the most fantastic thing about leiningen is to create a new project, is just one command you have a full project skeleton:
$> lein new app hello-clojure Generating a project called hello-clojure based on the 'app' template.
Now just open it inside your editor and start exploring our project! One of the things that you need to note is the file
project.clj, it is used by leiningen and it have important information to it build your project, like dependencies and the main function (the first function to run in our project). I think that if you already program other languages you know what this file is. For now you just need to think a lot about this file, in the future we’ll go deep.
Just to test our project, open the file
src/hello_clojure/core.clj and change the
println expression to any message:
(defn -main "Hello Clojure, I love U!" [& args] (println "Hello, World! I'm lovin Clojure"))
Now just go to your terminal and run your project with
$ > lein run Hello, World! I'm lovin Clojure
That’s wonderful, right? With lein we can run our tests too, just type
lein test, and you will run all tests of your project (leiningen app template comes with one test that’s easy to fix, it’s your mission try it).
We created our first project
hello-clojure, tested with
lein run, it’s time to build it! To build is very simple, just type
lein uberjar and wait it finish.
After it, you should have a
jar file inside
target/uberjar directory in your project, just navigate to ir, and run it as a jar:
$ > java -jar hello-clojure-0.1.0-SNAPSHOT-standalone.jar Hello, World! I'm lovin Clojure
Leiningen has a lot of templates to use with the command
leiningen new, it has a template for almost everything, and you can check for templates here. If you already waste some time in the internet looking for examples of skeletons of projects in X languages, this is for you! if you need a skeleton for CLI, just look at it, if you need something to webapps, just look at it! Its useful a lot.
We used the
app template because its the most basic template, and it represents a common structure for Clojure apps, it can be very useful on the next steps of our journey.
We learned a lot about Leiningen in this post, now we know how to create projects, run, test and build it… But we learned something more important, something that we’ll use a lot through our journey, we learned how to use REPL!
REPL is one of the most powerful tool that we’ll use, and it will help a lot while studying to quick evaluate expressions, get used to it, and you’ll be a great Clojure wizard.
If you have any questions that I can help you with, please ask! Send an email (otaviopvaladares at gmail.com), pm me on my Twitter or comment on this post!
Follow my blog to get notified every new post: