Thanks to visit codestin.com
Credit goes to www.slideshare.net

我把所有穿过的袜子在沙发上摆成一个二叉堆,每天回家便把袜子脱下来放在堆的最末尾,然后每次拿起两双袜子放在鼻子下闻闻,不断向上调整它在堆中的位置,直到比它的父亲更臭为止。这样,我每天早晨出门时便能方便地选出一双最不臭的袜子。
Clojure Practical Functional Approach on JVM Sun Ning 2011.11.28
Agenda Introduction to Clojure
Real World Clojure
Clojure People
 
Introduction to Clojure Functional Programming
Lisp Syntax
State Management
Macro
Java Interop
Introduction to Clojure Functional Programming
Lisp Syntax
State Management
Macro
Java Interop
Functional Programming Avoiding Mutable State
Functions as First-Class Values
Lambdas and Closures
Higher-Order Functions
Side-Effect-Free Functions
Recursion
Lazy vs. Eager Evaluation
Declarative vs. Imperative Programming
Functional Programming Avoiding Mutable State Better concurrency (no need for locking)
Persistent data structure zs = xs + ys
Functional Programming Functions as First-Class Values
Lambdas and Closures
Higher-Order Functions ;; integer as first class value (def a 10) ;; function as first class value (def b (fn [x] (+ x a))) ;; anonymous function #(+ a %) ;; function that generate a closure (def c (fn [x] #(+ x %))) ;; high order function (function as parameter) (def d (fn [f x] (f x a)))
Functional Programming Side-Effect-Free Functions ;; function that free of side effect (defn f [x] (+ x 1000)) ;; function with side effect (defn f [x] (println (str “logging: x=” x)) (+ x 1000)) ;; I/O operations are side effect (defn dump [writer data] (.write writer data))
Functional Programming Recursion Recursive Looping public int sumAll(int n) { int i = 1, s = 0; while(i <= n) { s = s + i; i++; } return s; } public int sumAll(int n) { if ( n > 0){ return n + sumAll(n-1); } else { return 0; } } (defn sum-all [n] (if (> n 0) (+ n (sum-all (dec n))) 0)) (defn sum-all2 [n s] (if (> n 0) ( recur  (dec n) (+ n s)) s)) (defn sum-all [n] (sum-all2 n 0)) java.lang.StackOverflowError Tail recursion
Functional Programming Lazy Evaluation ;; define data (def data [1 2 3 4 5]) ;; a function with side effect (defn side-effect-inc [i] (println i) (inc i)) ;; create lazy sequence with map (def lazy-data (map side-effect-inc data)) ;; consume first 2 items for lazy sequence (take 2 lazy-data) (1 2 3 4 5 2 3) (take 5 (range)) (take 5 (repeat “a”))
Functional Programming Declarative Programming DSL (cd &quot;/home/login&quot; (path &quot;/home/login/bin&quot; (run &quot;clojure&quot;))) (cd &quot;/home/login&quot; (path &quot;/home/login/bin&quot; (env &quot;JAVA_OPTS&quot; &quot;-XMaxPermSize=128m&quot; (run &quot;ant compile&quot;))))
Introduction to Clojure Functional Programming
Lisp Syntax
State Management
Macro
Java Interop
Syntax Symbol:  user/m
Character:  \a
Integer:  1 2 3
String:  “hello”
Keyword:  :world
Boolean:  true
Null:  nil
Collections Vector:  [1 2 3]
List:  (1 2 3)
Set:  #{1 2 3}

Clojure: Practical functional approach on JVM