Clojure
Table of Contents
1 Objective
Describe the basics and keep handy info of clojure language.
2 Sources
2.1 Brave Clojure
2.1.1 Chapter 1 - Getting Started
- Create a clojure project with Leiningen
lein new app clojure-noob
Generating a project called clojure-noob based on the 'app' template.
- Running the clojure project
lein run
I'm a little teapot!
- Build the project
lein uberjar
Created /home/moritz/workspace/clojure/clojure-noob/target/uberjar/clojure-noob-0.1.0-SNAPSHOT.jar Created /home/moritz/workspace/clojure/clojure-noob/target/uberjar/clojure-noob-0.1.0-SNAPSHOT-standalone.jar - Execute the JAR
java -jar target/uberjar/clojure-noob-0.1.0-SNAPSHOT-standalone.jar
I'm a little teapot!
- Execute code in REPL
lein repl
nREPL server started on port 45749 on host 127.0.0.1 - nrepl://127.0.0.1:45749 REPL-y 0.3.7, nREPL 0.2.12 Clojure 1.8.0 OpenJDK 64-Bit Server VM 1.8.0_191-8u191-b12-0ubuntu0.16.04.1-b12 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=> Bye for now!
3 Start from scratch
Instructions to create a project and start coding from official website.
3.1 Hello World
3.1.1 Directory Structure
mkdir -p basic
cp deps.edn basic
cd basic
mkdir -p src
ls
deps.edn src
3.1.2 Code
(ns hello (:require [java-time :as t])) (defn time-str "Returns a string representation of a datetime in the local time zone." [instant] (t/format (t/with-zone (t/formatter "hh:mm a") (t/zone-id)) instant)) (defn run [opts] (println "Hello world, the time is" (time-str (t/instant))))
3.2 Using Local Libraries
See Hello World.
3.2.1 Library Directory Structure
mkdir -p time-lib
cp deps.edn time-lib
cd time-lib
mkdir -p src
ls
deps.edn src
3.2.2 Code
(ns hello-time (:require [java-time :as t])) (defn now "Returns the current datetime" [] (t/instant)) (defn time-str "Returns a string representation of a datetime in the local time zone." [instant] (t/format (t/with-zone (t/formatter "hh:mm a") (t/zone-id)) instant))
3.2.3 Update Hello World
(ns hello (:require [hello-time :as ht])) (defn run [opts] (println "Hello world, the time is" (ht/time-str (ht/now))))
3.2.4 Update Dependencies
{:deps {time-lib/time-lib {:local/root "../time-lib"}}}