The functional paradigm has emerged in the 50s of the last century, but the industry has ignored it as the foundation for modern programming languages. Today, distributed applications are the rule and not the exception. Develop them in imperative languages make them complex, difficult to maintain and consequently expensive. Nowadays, the industry is being challenged by lean entrepreneurs, able to create, maintain and evolve extremely high quality functional languages. These entrepreneurs solve in a short time serious scalability, productivity, performance and maintenance issues with solid guarantees offered by the functional paradigm.
In this presentation, we introduce one of these new-generation languages. Clojure is a functional and dynamic language, whose design serves as inspiration for the development of several other modern languages like Java and Scala.
4. Object Oriented and Imperative
●
Object oriented
– Created for simulation systems (Simula 67)
– Object represent state and behavior which is a requirement for simulations
– Very good for user interface development (a simulation of concrete objects. Ex. button)
– But, is it good for everything else?
– By putting in a distributed environment you have clones and reports of gang rapes
● Imperative
– The art of programming in sequential steps to accomplish tasks
– Sometimes the list of steps is so large that frameworks should be used to make the
work practical
– By mixing it with object-oriented programming you have nitroglycerin (steps to manage
states)
5.
6. Java to Clojure
Public class StringUtils {
Public static boolean isBlank(String str) {
Int strlen;
if(str == null || (strlen = str.length()) == 0) {
Return true;
}
For (int i = 0; I < strlen; i++ ) {
if((Character.isWhitespace(str.charAt(i)) == false)) {
Return false;
}
}
Return true;
}
}
(defn blank? [s] (every? #(Character/isWhitespace %) s))
9. What Happened Hildeberto?!
1.Oracle stopped supporting Glassfish
2.Pushed to look for alternatives
3.Started migrating to Jboss (Nightmare!!!)
4.Realized that Java EE is a broken standard
5.Pushed to look for alternatives once again
6.Found Scala + Akka + Play (Wonderfull!!!)
7.But Scala is pretty damn complex :-(
13. Clojure
● Dialect of LISP (List Processing)
● Dynamically typed but strongly typed
● Functional (not purely functional, a bit more)
● Host interoperability
● Software transactional memory
● Everything is an expression
● Compiled
● Macros
● Lazy sequences
By Rich Hickey
14. LISP?
● Created by John McCarthy
● He coined the term “Artificial Intelligence”
● Exceptionally intelligent
● Created the “if” structure.
John McCarthy
18. Basic Side-by-Side with Java
Clojure
(not k)
(inc a)
(/ (+ x y) 2)
(instance? Java.utils.List al)
(if (not a) (inc b) (dec b))
(Math/pow 2 10)
(.someMethod obj “foo”
(.otherMethod otherObj o))
Java
!k
a++, ++a, a += 1, a + 1
(x + y) / 2
al instanceof java.util.List
!a ? b + 1 : b – 1;
Math.pow(2, 10);
obj.someMethod(“foo”,
otherObj.otherMethod(o));
19. Homoiconicity
● Code as data
(if (odd? 2) “odd” “even”)
● Significant simplification (less memorization)
(Symbol arg1 arg2 … argN)
You already learned 80% of what it takes to program in
Clojure!
● Metaprogramming facilities (macros)
(def n (fn […] …)) => (defn n […] ...)
● Represents the abstract syntax tree directly
20. Abstract Syntax Tree Example
● RequiresRole && userHasRole(“MODIFY”, assetId)
AND
requiresRole Call
userHasHole assetId
“MODIFY”
● (and requiresRole (userHasRole “MODIFY” assetId))
23. Clojure Evaluation
Code
file
Reader
Evaluator /
compiler
JVM
Effect
Not happy with the result? Send more code to the reader.
Characters
Data structures
Bytecode
Once started,
it keeps running
while you work.
Local / Remote
TCP/IP
Connection
Program
Characters
Data
structures
Macro
Data
structures
27. Community
● Everything is on GitHub:
https://github.com/clojure
● 70 user groups; only one in Brazil (São Paulo)
● Mailing list with ~10k users (everybody is nice)
● Position on Tiobe: 62