A simple project to put Clojure concepts into practice.
- Java
- Leiningen
Execute REPL (Read-Eval-Print Loop)
$ lein repl
You should see something like this:
nREPL server started on port #### on host 127.0.0.1
REPL-y 0.5.1, nREPL 0.8.3
Clojure 1.10.3
Java HotSpot(TM) 64-Bit Server VM 17.0.1+12-LTS-39
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
Now inside REPL we'll execute the main class with:
(-main)
Also, we can execute
(+ 1 2 3 4)
;; 10
(* 1 2 3 4)
;; 24
(first [3, 7, 1, 2])
;; 3
(do (println "Hello World")
(- 10 5 2))
;; Hello World
;; 3
💫 Happy Hacking!
Some examples and points of interest to stay connected.
In Java we'll' use the
static
keyword because Clojure is a functional programming language.
On Java:
public static final String name = "Gustavo Flôr"
On Clojure:
(def name "Gustavo Flôr")
We don't need to define an access or var type, we just define the symbol and value and that's it.
Also, in Clojure we don't create variables, we bind a value to a symbol. So the closes thing we can do in Java is to use the "final" keyword.
On Java:
public static Object sayHello(final String person) {
return String.format("Hello %s!", person);
}
On Clojure:
(defn say-hello
[person]
(str "Hello " person "!"))
First, we need to pay attention at the keyword "function", on Java we don't have functions we create methods on a class.
Also, on Clojure we don't need to define a return type, because it always returns the last form evaluated (in this example, as we only have one expression, this will be the return).
If you want more, read Clojure for the Brave and True