On Functional Programming - A Clojurian Perspective

685 views
613 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
685
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

On Functional Programming - A Clojurian Perspective

  1. 1. On Functional Programming A Clojurian Perspective Raju Gandhi
  2. 2. Disclaimers
  3. 3. Disclaimers I tend to speak fast
  4. 4. Disclaimers I tend to speak fast I may have an accent
  5. 5. (struct-map Speaker :name "raju", :pronunciation "/raa-jew/", :description ["java/ruby developer", "technophile", "language geek"], :profiles {:twitter "looselytyped" :facebook "raju.gandhi"})
  6. 6. About us... Small consulting/training/mentoring shop Based out of Ohio and Arizona Specialize in open-source technologies - Java/Ruby/Rails/Groovy/Grails
  7. 7. Clojure? Lisp on the JVM Dynamic Excellent concurrency support Strong Java inter-op Lazy*
  8. 8. Clojure Syntax A whirlwind tour
  9. 9. 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}
  10. 10. 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}
  11. 11. 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}
  12. 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}
  13. 13. 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}
  14. 14. LISt Processing ;can be a regular function ;Yes! + is a function :) (+ 1 2 3) ;or a special form (if (< x 3) "less than 3" "or not") ;or a macro (defn say-hello [name] (str "Hello, " name))
  15. 15. LISt Processing ;can be a regular function ;Yes! + is a function :) (+ 1 2 3) ;or a special form (if (< x 3) "less than 3" "or not") ;or a macro (defn say-hello [name] (str "Hello, " name))
  16. 16. LISt Processing ;can be a regular function ;Yes! + is a function :) (+ 1 2 3) ;or a special form (if (< x 3) "less than 3" "or not") ;or a macro (defn say-hello [name] (str "Hello, " name))
  17. 17. LISt Processing ;can be a regular function ;Yes! + is a function :) (+ 1 2 3) ;or a special form (if (< x 3) "less than 3" "or not") ;or a macro (defn say-hello [name] (str "Hello, " name))
  18. 18. LISt Processing ;can be a regular function ;Yes! + is a function :) (+ 1 2 3) ;or a special form (if (< x 3) "less than 3" "or not") ;or a macro (defn say-hello [name] (str "Hello, " name))
  19. 19. LISt Processing ;can be a regular function ;Yes! + is a function :) (+ 1 2 3) ;or a special form (if (< x 3) "less than 3" "or not") ;or a macro (defn say-hello [name] (str "Hello, " name))
  20. 20. LISt Processing ;can be a regular function ;Yes! + is a function :) (+ 1 2 3) ;or a special form (if (< x 3) "less than 3" "or not") ;or a macro (defn say-hello [name] (str "Hello, " name))
  21. 21. Homoiconicity ;defining a function (defn say-hello [name] (str "Hello, " name))
  22. 22. Homoiconicity ;defining a function (defn say-hello [name] (str "Hello, " name))
  23. 23. Homoiconicity ;defining a function (defn say-hello [name] (str "Hello, " name))
  24. 24. Homoiconicity ;defining a function (defn say-hello [name] (str "Hello, " name))
  25. 25. Homoiconicity code == data
  26. 26. What is FP?
  27. 27. What is FP? What is OOP???
  28. 28. What is FP? What is OOP??? Functional programming
  29. 29. What is FP? What is OOP??? Functional programming Functions are first class citizens
  30. 30. Why FP? Compartmentalize Better re-use Referential Transparency Easier to test Easier to parallelize
  31. 31. Clojure’s Approach Side effects are explicit State manipulation via Persistent data-structures Multiple reference types with appropriate semantics
  32. 32. Declaring Functions ;explicit definition (defn times-2 "Multiplies its arg by 2" [n] (* 2 n))
  33. 33. Declaring Functions ;alternate approach ;no docs though (def times-2 (fn [n] (* 2 n)))
  34. 34. Declaring Functions ;anonymous function (map #(* 2 %) [1 2 3])
  35. 35. Declaring Functions ;anonymous function (map #(* 2 %) [1 2 3])
  36. 36. Consuming Functions ;map takes ([f coll] ...) (map times-2 [1 2 3]) ;> (2 4 6)
  37. 37. Consuming Functions ;map takes ([f coll] ...) (map #(* 2 %) [1 2 3]) ;> (2 4 6)
  38. 38. Consuming Functions ;reduce takes ([f coll] ...) (reduce + [1 2 3]) ;> 6
  39. 39. 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
  40. 40. A Small Digression (let [x 1, y 2] (str x " " y)) ;> ”1 2” (let [[x y] [1 2]] (str x " " y)) ;> "1 2" (let [[x & more] [1 2 3]] (str x " " more)) ;> "1 (2 3)"
  41. 41. Looping (defn min-in-coll [x & more] (loop [min x [f & others] more] (if f (recur (if (< min f) min f) others) min))) ;(min-in-coll [3 4 2 -1]) ;> -1
  42. 42. Thanks!

×