Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency...

46
Finding Clojure Raju Gandhi Thursday, October 11, 12

Transcript of Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency...

Page 1: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Finding ClojureRaju Gandhi

Thursday, October 11, 12

Page 2: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Disclaimers

I tend to speak fast

I may have an accent

Thursday, October 11, 12

Page 3: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

(def speaker { :name "raju",

:pronunciation "/raa-jew/", :description ["java/ruby developer", "technophile", "language geek"], :profiles {:twitter "looselytyped" :facebook "raju.gandhi"}})

Thursday, October 11, 12

Page 4: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

About us...

Small consulting/training/mentoring shop

Based out of Ohio and Arizona

Specialize in open-source technologies - Java/Ruby/Rails/Groovy/Grails

Thursday, October 11, 12

Page 5: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Lisp on the JVM

Dynamic

Excellent concurrency support

Strong Java inter-op

Lazy*

Clojure?

Thursday, October 11, 12

Page 6: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Clojure Syntax

A whirlwind tour

Thursday, October 11, 12

Page 7: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Clojure Syntax

;lists - these are special'(+ 1 2 1/3);a comment["this" "is" "a" "vector"];commas are whitespace{:yes true, :no false, :null nil};sets#{\a \e \i \o \u}

Thursday, October 11, 12

Page 8: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Clojure Syntax

;lists - these are special'(+ 1 2 1/3);a comment["this" "is" "a" "vector"];commas are whitespace{:yes true, :no false, :null nil};sets#{\a \e \i \o \u}

Thursday, October 11, 12

Page 9: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Clojure Syntax

;lists - these are special'(+ 1 2 1/3);a comment["this" "is" "a" "vector"];commas are whitespace{:yes true, :no false, :null nil};sets#{\a \e \i \o \u}

Thursday, October 11, 12

Page 10: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Clojure Syntax

;lists - these are special'(+ 1 2 1/3);a comment["this" "is" "a" "vector"];commas are whitespace{:yes true, :no false, :null nil};sets#{\a \e \i \o \u}

Thursday, October 11, 12

Page 11: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Clojure Syntax

;lists - these are special'(+ 1 2 1/3);a comment["this" "is" "a" "vector"];commas are whitespace{:yes true, :no false, :null nil};sets#{\a \e \i \o \u}

Thursday, October 11, 12

Page 12: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

LISt Processing

;can be a regular function;Yes! + is a function :)(+ 1 2 3);or a macro(defn say-hello [name] (str "Hello, " name));or a special form(if (< x 3) "less than 3" "or not")

Thursday, October 11, 12

Page 13: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

LISt Processing

;can be a regular function;Yes! + is a function :)(+ 1 2 3);or a macro(defn say-hello [name] (str "Hello, " name));or a special form(if (< x 3) "less than 3" "or not")

Thursday, October 11, 12

Page 14: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

LISt Processing

;can be a regular function;Yes! + is a function :)(+ 1 2 3);or a macro(defn say-hello [name] (str "Hello, " name));or a special form(if (< x 3) "less than 3" "or not")

Thursday, October 11, 12

Page 15: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

LISt Processing

;can be a regular function;Yes! + is a function :)(+ 1 2 3);or a macro(defn say-hello [name] (str "Hello, " name));or a special form(if (< x 3) "less than 3" "or not")

Thursday, October 11, 12

Page 16: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

LISt Processing

;can be a regular function;Yes! + is a function :)(+ 1 2 3);or a macro(defn say-hello [name] (str "Hello, " name));or a special form(if (< x 3) "less than 3" "or not")

Thursday, October 11, 12

Page 17: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

LISt Processing

;can be a regular function;Yes! + is a function :)(+ 1 2 3);or a macro(defn say-hello [name] (str "Hello, " name));or a special form(if (< x 3) "less than 3" "or not")

Thursday, October 11, 12

Page 18: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

LISt Processing

;can be a regular function;Yes! + is a function :)(+ 1 2 3);or a macro(defn say-hello [name] (str "Hello, " name));or a special form(if (< x 3) "less than 3" "or not")

Thursday, October 11, 12

Page 19: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Homoiconicity

;defining a function(defn say-hello [name] (str "Hello, " name))

Thursday, October 11, 12

Page 20: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Homoiconicity

;defining a function(defn say-hello [name] (str "Hello, " name))

Thursday, October 11, 12

Page 21: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Homoiconicity

;defining a function(defn say-hello [name] (str "Hello, " name))

Thursday, October 11, 12

Page 22: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Homoiconicity

;defining a function(defn say-hello [name] (str "Hello, " name))

Thursday, October 11, 12

Page 23: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Homoiconicity

code == data

Thursday, October 11, 12

Page 24: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

What is FP?

What is OOP???

Functional programming

Functions are first class citizens

Thursday, October 11, 12

Page 25: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Why FP?

Compartmentalize

Better re-use

Referential Transparency

Easier to test

Easier to parallelize

Thursday, October 11, 12

Page 26: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Clojure’s Approach

Side effects are explicit

State manipulation via

Persistent data-structures

Multiple reference types with appropriate semantics

Thursday, October 11, 12

Page 27: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Declaring Functions

;explicit definition(defn times-2 "Multiplies its arg by 2" [n] (* 2 n))

Thursday, October 11, 12

Page 28: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Declaring Functions

;alternate approach;no docs though(def times-2 (fn [n] (* 2 n)))

Thursday, October 11, 12

Page 29: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Declaring Functions

;anonymous function(map #(* 2 %) [1 2 3])

Thursday, October 11, 12

Page 30: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Declaring Functions

;anonymous function(map #(* 2 %) [1 2 3])

Thursday, October 11, 12

Page 31: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Consuming Functions

;map takes ([f coll] ...)(map times-2 [1 2 3]);> (2 4 6)

Thursday, October 11, 12

Page 32: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Consuming Functions

;map takes ([f coll] ...)(map #(* 2 %) [1 2 3]);> (2 4 6)

Thursday, October 11, 12

Page 33: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Consuming Functions

;reduce takes ([f coll] ...)(reduce + [1 2 3]);> 6

Thursday, October 11, 12

Page 34: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Functions Everywhere

([4 5 6] 0);> 4(#{\a \e \i \o \u} \a);> \a({:yes true, :no false, :null nil} :yes);> true(:yes {:yes true, :no false, :null nil});> true

Thursday, October 11, 12

Page 35: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Manipulating Data

(def lst ‘(1 2 3 4))(first lst); 1(second lst); 2(nth lst 2); 3(last lst); 4

Thursday, October 11, 12

Page 36: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Manipulating Data

; (def lst ‘(1 2 3 4))(list (first lst) (second lst) (nth lst 2) 5 (last lst))

; (1 2 3 5 4)

Thursday, October 11, 12

Page 37: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Manipulating Code As Data

(+ 1 2); 3‘(+ 1 2); (+ 1 2)(eval ‘(+ 1 2)); 3

Thursday, October 11, 12

Page 38: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

(defn say-hello [name] (str “Hello ” name)); #'user/say-hello(say-hello “Raju”); “Hello Raju”

Manipulating Code As Data

Thursday, October 11, 12

Page 39: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

‘(defn say-hello [name] (str “Hello ” name)); (defn say-hello [name] (str "Hello " name))(eval *1); #'user/say-hello(*1 “Raju”); “Hello Raju”

Manipulating Code As Data

Thursday, October 11, 12

Page 40: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

(def lst ‘(defn say-hello [name] (str “Hello ” name))); #'user/lst(first lst); defn(second lst); say-hello(nth lst 2); [name](last lst); (str "Hello " name)

Manipulating Code As Data

Thursday, October 11, 12

Page 41: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

;(def lst ‘(defn say-hello [name] (str “Hello ” name)))(def logged-say-hello (apply list (first lst) (second lst) (nth f lst) (list '(println "Args: " name) (last lst)))); #'user/logged-say-hello

logged-say-hello(defn say-hello [name] (println "Arg: " name) (str "Hello " name))

Manipulating Code As Data

Thursday, October 11, 12

Page 42: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

; logged-say-hello; (defn say-hello [name] (println "Arg: " name) (str "Hello " name))

(eval logged-say-hello); #'user/say-hello(say-hello "Catherine"); Args: Catherine; "Hello Catherine"

Manipulating Code As Data

Thursday, October 11, 12

Page 43: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

(defmacro log-it [fn-name args & body] `(defn ~fn-name ~args (println "Args are: " ~args) ~@body))

(log-it say-hello [name] (str "Hello " name)); #'user/say-hello

(say-hello "Raju"); Args are: [Raju]; "Hello Raju"

Macros

Thursday, October 11, 12

Page 44: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Lots More

Immutable/Persistent data structures

Reference types for concurrency

Refs

Agents

Atoms

...

Thursday, October 11, 12

Page 46: Finding Clojured8ndl.org/Finding_Clojure.pdf · Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy* Clojure? Thursday, October 11, 12

Thanks!

Thursday, October 11, 12