Skip to main content

Your first Clojure Project in 5 minutes

Clojure is a dynamic functional language, and is a dialect of Lisp with a priority of concurrency. It can run on top of the JVM (Java Virtual Machine), CLR (Common Language Runtime) and Javascript platforms like NodeJS through ClojureScript.

Its pioneer is Rich Hickey, and if you have not heard his talks, I recommend this one on "Simple made easy". You can learn about the current state of adoption of the language in this Infoworld article.

If you'd like to understand why Clojure is a good choice to learn, Uncle Bob can tell you. Tl;dr "I began to realize that this language was the easiest, most elegant, least imposing language I had ever used – and not by a small margin.".

If you have never interacted with Lisp, Clojure can have a steep learning curve. The aim of this post is not to give a level of understanding of how Clojure operates. Rather, it exists to help people realise they can achieve a basic program very quickly even if they do not yet understand the inner workings.

This tutorial assumes you have Homebrew and Java 1.6 or newer installed. It’s based on the Brave Clojure book which is an incredible in depth guide, read it after this to dive deeper. Let's go! Open your command line and type anything in bold without quotes. Bold with quotes represent file/folder names and code not to be typed.

Create & Share Hello World

1. Automate your project: brew install leiningen *What is Leiningen?

2. Start a project: lein new app my-clojure-project

3. Go to the project folder: cd my-closure-project

4. Run the project: lein run

5. Create a Java executable: lein uberjar *What's a Java executable?

6. Find the executable file: "my-closure-project-0.1.0-SNAPSHOT-standalone" in "/target/uberjar"

Understand main parts

1. Main program file: "core.clj" in "/src/my-clojure-project" *What is core.clj?

2. Project identifier (namespace): "ns my-clojure-project.core" *What's a namespace?

3. Program entry point: "defn -main"

4. Action taken: "(println "Hello, World!")" *Why the brackets?


Interact with your program through a REPL

1. Start the REPL from command line: lein repl *What's a REPL?

2. Notice you're in the project namespace: "my-clojure-project.core"

3. Run the entry point: type (-main) and press enter

Bonus: Do stuff with the REPL without a project

1. Start the REPL again if you stopped it: lein repl

2. Type something to evaluate: (+ 1 3)

3. Press enter and observe the result: "6"

4. Type something to use a built in function: (first [1 5 7]) *What is "first"?

5. Press enter and observe the result: "1" 



Comments

Popular posts from this blog

Startups as systems

The process of creation of a startup is the creation of a system. “A system is an interconnected set of elements that is coherently organised in a way that achieves something”. Your ability to influence that system extends to the elements (called “stocks”) you chose, the way in which they are organised (called “flows”), and the achievement itself. There is also a feedback loop in most systems, which takes time. Whether it’s adding a new feature, changing pricing, architecture, go-to-market and so on, it needs to be planned, implemented and measured for impact. Your goal as a startup is to get to product-market fit (“being in a good market with a product that can satisfy that market.”). Thus the achievement, or output, is to produce a product fit for a given market using a model which both satisfies the market and is profitable for the producer. The race is to do that before you run out of money, or grit, or both. Since feedback loops take time, and since changes to your system...