Download - Clojure Small Intro

Transcript
Page 1: Clojure Small Intro

Clojure

John Vlachoyiannis@jonromero

[email protected]://jon.is.emotionull.com

Page 2: Clojure Small Intro

What is Clojure?

Page 3: Clojure Small Intro

Ok, what is Lisp?

Page 4: Clojure Small Intro

“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".

Page 5: Clojure Small Intro

“LISP stands for: Lots of Insane Stupid Parentheses”

AnonymousAnonymous

Page 6: Clojure Small Intro

The Truth about Lisp

Page 7: Clojure Small Intro

LISLISt PProcessing

● Second oldest high-level language (first is Fortran)

● Code as Data (Homoiconic)● Perfect for Domain-specific languages

(DSL)● Exploratory programming

Page 8: Clojure Small Intro

Clojure

● Lisp in JVM● Concurrent programming● Dynamic Development (REPL)● Lazy sequences● No side effects (almost)

Page 9: Clojure Small Intro

Enter Clojure

Page 10: Clojure Small Intro

Everything is code

Page 11: Clojure Small Intro

(println "Hello World")

function argument

Page 12: Clojure Small Intro

Everything is data

Page 13: Clojure Small Intro

(println "Hello World")

symbol string

list

Page 14: Clojure Small Intro

● 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]|

Page 15: Clojure Small Intro

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}

Page 16: Clojure Small Intro

“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

Page 17: Clojure Small Intro

clojure might be a better java than java

Page 18: Clojure Small Intro

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;

}}

Page 19: Clojure Small Intro

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;

}}

Page 20: Clojure Small Intro

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;

}

Page 21: Clojure Small Intro

public isBlank(str) {if (str == null || (strLen = str.length()) == 0) {

return true;}

every (ch in str) {Character.isWhitespace(ch);

} return true;

}

Page 22: Clojure Small Intro

public isBlank(str) {every (ch in str) {

Character.isWhitespace(ch);}

}

Page 23: Clojure Small Intro

(defn blank? [s] (every? #(Character/isWhitespace %) s))

Page 24: Clojure Small Intro

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

Page 25: Clojure Small Intro

Clojure is a functional language

● Functions are first-class objects● Data is immutable● Functions are pure

Page 26: Clojure Small Intro

So what?

Page 27: Clojure Small Intro

● 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

Page 28: Clojure Small Intro

user=> (println "hello world")| hello world-> nil

Page 29: Clojure Small Intro

user=> (defn hello [name] (str "Hello, " name))

#'user/hello

Page 30: Clojure Small Intro

(hello "Clojure")

Page 31: Clojure Small Intro

(.toUpperCase “hello”)

Page 32: Clojure Small Intro

(str “hello” “ “ “world”)

Page 33: Clojure Small Intro

(+ 1 3 4 (* 5 6))

Page 34: Clojure Small Intro

(defn greeting"Returns a greeting of the form 'Hello, username.'"[username](str "Hello, " username))

Page 35: Clojure Small Intro

(greeting "world")

Page 36: Clojure Small Intro

user=> (doc greeting)-------------------------exploring/greeting

([username])Returns a greeting of the form 'Hello, username.'

Page 37: Clojure Small Intro

(defn is-small? [number](if (< number 100)

"yes" ))

Page 38: Clojure Small Intro

(is-small? 50)"yes"

Page 39: Clojure Small Intro

(is-small? 50000)nil

Page 40: Clojure Small Intro

(defn is-small? [number](if (< number 100)

"yes" "no" ))

Page 41: Clojure Small Intro

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)

Page 42: Clojure Small Intro

Java in Clojure

● Yeap

Page 43: Clojure Small Intro

Clojure in Java

● Yeap

Page 44: Clojure Small Intro

Clojure in Clojure

● Yeap, yeap

Page 45: Clojure Small Intro

Java in Clojure

java new Widget(“foo”)

clojure (Widget. “foo”)

java dialog.show()

clojure (.show dialog)

Page 46: Clojure Small Intro

Tools

● Emacs + SLIME● ViMClojure ● Enclojure + Intellij IDEA● Counterclockwise + Eclipse● Leiningen

Page 47: Clojure Small Intro

Clojure is not a just a new language

Page 48: Clojure Small Intro

Is another, simplier way to solve problems

Page 49: Clojure Small Intro

Thanks! Questions?@jonromero