Getting started with Clojure
My first steps in learning Clojure and setting things up.
Get inspired
Start with Leiningen
Since Clojure is a Jar file there is no Clojure binary to install. You just need a Java JVM and Leiningen.
Leiningen is like Gemfile and Rakefile in Ruby, plus much more.
brew install leiningen
(or use your preferred package manager)
Start a new project
lien project new playground
By default a command-line app is generated. You can get a different starting point by specifying a template, for example Figwheel for ClojureScript, Play for dekstop and mobile games, or Luminus for websites.
A commandline app is packaged as an uberjar, that is a jar file which includes all it’s dependencies.
You can build the uberjar:
lein uberjar
and run it using java -jar playground-0.1.0-standalone.jar
.
REPL
The REPL (Read Eval Print Loop), like Irb in Ruby, can be started with lein
:
lien repl
In the REPL we can requie and run our newly created app:
(require 'playground.core) ; require the app
(playground.core/-main) ; run the app
In the source playground.core
is a namespace and -main
is a function. The -main
function is used as an entry point for the uberjar, it is passed the commandline arguments as its arguments.
The REPL is the best place to start experimenting with Clojure syntax.
Tests
This is just here for compleness, I don’t intend to write any tests for quite a while.
lien test
Run
lien run
For more detailed information about Lein see the leiningen tutorial.