2. Assumptions
Some experience with Java development
Little or no experience with Clojure
You want to try something different!
Haskell, Erlang and
Scala seem hairy!
13. Clojure concepts
Encourages Pure Functional approach
- use STM to change state
Functions as first class citizens
- functions as arguments as they return a value
Make JVM interoperation simple
- easy to use your existing Java applications
14. A better Lisp !
Sensible () usage
Sensible macro names
JVM Interoperability
Lisp is the 2nd oldest programming language still in use
27. Its all byte code in the end..
Any object in Clojure is just a regular java
object
Types inherit from:
java.lang.object
28. What class is that...
(class "Jr0cket")
=> java.lang.String
(class
(defn hello-world [name]
(str "Hello cruel world")))
=> clojure.lang.Var
Using (type …) you can discover either the metadata or class of something
33. Defining a data structure
( def my-data-structure [ data ] )
( def days-of-the-week
[“Monday” “Tuesday” “Wednesday”])
You can dynamically redefine what the name binds to, but the vector is immutable
34. Really simple data structure
(def jr0cket
{:first-name "John",
:last-name "Stevenson"})
Using a map to hold key-value pairs.
A good way to group data into a meaningful concept
48. Simplifying with doto
doto evaluates the first expression in a chain of expressions and saves it in a
temporary variable. It then inserts that variable as the first argument in each of the
following expressions. Finally, doto returns the value of the temporary variable.
49. Working with Java
Java Classes
● fullstop after class name
(JFrame. )
(Math/cos 3) ; static method call
Java methods
● fullstop before method name
(.getContentPane frame) ;;method name first
(. frame getContentPane) ;;object first
56. Maven
Just like any other Java project
Step 1)
Add Clojure library dependency to your
POM
Step 2)
Download the Internet !!!
57. Leiningen
lein new
lein deps
lein repl
lein jack-in
● Create a new Clojure project
● Download all dependencies
● Start the interactive shell (repl)
● Start a REPL server
leiningen.org
70. Recursive functions
● Functions that call
themselves
● Fractal coding
● Tail recursion
● Avoids blowing the
stack
● A trick as the JVM
does not support tail
recursion directly :-(
71. Recursion – managing memory
(defn recursive-counter [value]
(print value)
(if (< value 1000)
(recur (+ value 4))))
(recursive-counter 100)