Download - Programming with Freedom & Joy
Programming with Freedom & Joy
Confessions of an ExJavaEE enthusiast that found enlightenment through Clojure
Hildeberto Mendonça@htmfilho
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)
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))
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 :-(
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
LISP?
● Created by John McCarthy● He coined the term “Artificial Intelligence”● Exceptionally intelligent● Created the “if” structure.
John McCarthy
Learn Once Code Everywhere
Java Clojure Bytecode JVM
C# Clojure CIL code CLR
Clojure ClojureScript JavaScript JS Engine
Python Clojure Python Cpython
● Hosted languagehttp://clojure.org
http://clojure.org/clojureclr
http://clojure.org/clojurescript
https://github.com/halgari/clojure-py
Syntax in One Slide
● Numbers – 3454.23● Ratios – 3/56● Strings - “Carnaval”● Characters - \a \b \return● Symbols – alex b c● Keywords - :a :b :c● Booleans – true false● Null – nil● Regex patterns #”a*b”
● Lists (1 2 3 4 5 4)● Vectors [1 2 3 3 5]● Maps {:a 1 :b 2 :c 2}● Sets #{1 2 3 4 5}● Prefixes ' #() @ ~ ~@● Macros● ; (documentation)
It is simple because it is all about interpretation of data structures, not text
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));
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
Abstract Syntax Tree Example
● RequiresRole && userHasRole(“MODIFY”, assetId)
AND
requiresRole Call
userHasHole assetId
“MODIFY”
● (and requiresRole (userHasRole “MODIFY” assetId))
Data Structures
● Persistent (versioned)● Immutable
T1 T2
Structural Sharing
(conj [1 2 3 4] 5)[1 2 3 4]
Traditional Evaluation
CodeText
Compiler
Executable.class/.jar
Run java JVM
Effect
Not happy with the result? Start over.
Characters
Bytecode
Clojure Evaluation
Codefile
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 / RemoteTCP/IP
Connection
Program
Characters
Data structures
Macro
Data structures
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