clojure small intro
DESCRIPTION
Presentation for JHUG in GreeceTRANSCRIPT
What is Clojure?
Ok, what is Lisp?
“Lisp is worth learning for the profound enlightenment experience you will have
when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use
Lisp itself a lot."
Eric S. Raymond, "How to Become a Eric S. Raymond, "How to Become a Hacker".Hacker".
“LISP stands for: Lots of Insane Stupid Parentheses”
AnonymousAnonymous
The Truth about Lisp
LISLISt PProcessing
● Second oldest high-level language (first is Fortran)
● Code as Data (Homoiconic)● Perfect for Domain-specific languages
(DSL)● Exploratory programming
Clojure
● Lisp in JVM● Concurrent programming● Dynamic Development (REPL)● Lazy sequences● No side effects (almost)
Enter Clojure
Everything is code
(println "Hello World")
function argument
Everything is data
(println "Hello World")
symbol string
list
● Integers – 1234567891234● Doubles – 3.14. BigDecimals – 3.14M● Ratios – 22/4● Strings – '”foo”, Character – \a \b \c● Symbols – foo, Keywords :foo● Booleans – true false, Null – nil● Regex patterns – #”[a-zA-Z0-9]|
Data structures
● Lists● (1 2 3 4), (foo bar baz), (list 1 2 3)
● Vectors● [1 2 3], [foo bar], (vector 1 2 3)
● Maps● {:a 1 :b 2 :c 3}
● Sets ● #{foo bar}
“It is better to have 100functions operate on one data
structure than to have 10functions operate on 10 data
Structures.”
Alan J. PerlisAlan J. Perlis
clojure might be a better java than java
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;
}}
public class StringUtils {public isBlank(str) {
if (str == null || (strLen = str.length()) == 0) {return true;
}for (i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;}
}return true;
}}
public isBlank(str) {if (str == null || (strLen = str.length()) == 0) {
return true;}for (i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {return false;
}}return true;
}
public isBlank(str) {if (str == null || (strLen = str.length()) == 0) {
return true;}
every (ch in str) {Character.isWhitespace(ch);
} return true;
}
public isBlank(str) {every (ch in str) {
Character.isWhitespace(ch);}
}
(defn blank? [s] (every? #(Character/isWhitespace %) s))
Clojure vs Java code
● Side-effect free ● Easy to (unit) test● Lazy collection● Any element● Slower● Data manipulation● Exploratory
● Error prone● Not so easy● Only one element● Only with chars● Faster● If/for/while code ● Design Is Law
Clojure is a functional language
● Functions are first-class objects● Data is immutable● Functions are pure
So what?
● Simple: no loops, variables or mutable state
● Thread-safe: no locking● Parallelizable: map/reduce anyone?● Generic: data is always data● Easy to test: same input, same output
user=> (println "hello world")| hello world-> nil
user=> (defn hello [name] (str "Hello, " name))
#'user/hello
(hello "Clojure")
(.toUpperCase “hello”)
(str “hello” “ “ “world”)
(+ 1 3 4 (* 5 6))
(defn greeting"Returns a greeting of the form 'Hello, username.'"[username](str "Hello, " username))
(greeting "world")
user=> (doc greeting)-------------------------exploring/greeting
([username])Returns a greeting of the form 'Hello, username.'
(defn is-small? [number](if (< number 100)
"yes" ))
(is-small? 50)"yes"
(is-small? 50000)nil
(defn is-small? [number](if (< number 100)
"yes" "no" ))
Solving problems
● Experiment with the problem ● Create your data structures ● Data transformations● Write code that writes code for you
(macros)● Create a mini language (a DSL)
Java in Clojure
● Yeap
Clojure in Java
● Yeap
Clojure in Clojure
● Yeap, yeap
Java in Clojure
java new Widget(“foo”)
clojure (Widget. “foo”)
java dialog.show()
clojure (.show dialog)
Tools
● Emacs + SLIME● ViMClojure ● Enclojure + Intellij IDEA● Counterclockwise + Eclipse● Leiningen
Clojure is not a just a new language
Is another, simplier way to solve problems
Thanks! Questions?@jonromero