Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming with Clojure

Functional Programming with Clojure

[MSDN@USF] Functional Programming with Clojure

Speaker: Elijah Malaby

https://www.facebook.com/groups/MSDNUSF/

David Suarez

March 02, 2017
Tweet

More Decks by David Suarez

Other Decks in Technology

Transcript

  1. What is clojure? ˆ Functional Programming Language ˆ First-class functions/lambdas

    ˆ Pure functions ˆ Lisp ˆ REPL Oriented Development/Interactive Programming ˆ Language is extensible 1
  2. What is EDN? ˆ Notation for writing data structures ˆ

    Human readable and precise enough for programming 2
  3. What is EDN? ˆ EDN divides its world into two

    groups: 1. Collections - Hold other atoms and collections 2. Atoms - Everything else 3
  4. Atoms ˆ Numbers ˆ Integers: 1, 2, 3, 4 ˆ

    Floats: 1.1, 1.2, 3.5, 76000.2 ˆ Rationals: 3/7, 4/5 ˆ Characters ˆ \a, \b, \c, . . . , \z, \1, \2, etc. . . ˆ Strings ˆ "Hello World" ˆ Symbols ˆ these, are, all, symbols ˆ Keywords ˆ :these, :are, :keywords 4
  5. Collections ˆ Lists ˆ (1 2 3 4), ("Hello" "World"),

    ("Hello", "World"), ("Hello" ,"World") ˆ Vectors ˆ [1 2 3 4], [1, 2, 3, 4] ˆ Maps ˆ {:a 1 :b 2}, {:a 1, :c 3} ˆ Sets ˆ #{:a :b :c :d} 5
  6. Basic Clojure Programming ˆ Polish Notation: ˆ 1 + 2

    + 3 becomes (+ 1 2 3) ˆ 1 / 2 becomes (/ 1 2) ˆ 1 < 2 becomes (< 1 2) ˆ doSomethingToX(x) becomes (doSomethingToX x) ˆ Or even (do-something-to-x x) 6
  7. If expressions (if true :a :b) => :a (if false

    :a :b) => :b (if (< 1 2 3) (print "Hello World") (print "Math is broken")) 7
  8. Let expressions (let [a 1] (+ a a)) (let [a

    1 b 2] (+ a b)) (let [a 1 b (if (= a 1) 2 3)] (+ a b)) 8
  9. Dening functions (defn add-numbers [a b c] (+ a b

    c)) (add-numbers 4 5 6) => 15 9
  10. Functional Programming ˆ Passing functions as arguments to other functions

    ˆ Make more reusable code ˆ Pure Functions/Referential Transparency ˆ Functions should be self-contained ˆ Only job is to make return value using arguments 10
  11. Map Filter Reduce: Functional Programming Tools ˆ Map: Apply a

    function to each element in a collection ˆ (map inc [1 2 3 4]) => [2 3 4 5] ˆ (map (fn [x] (* x 2)) [1 2 3 4]) => [2 4 6 8] ˆ Filter: Select certain elements in a collection ˆ (lter odd? [1 2 3 4 5 6]) => (1 3 5) ˆ (lter even? [1 2 3 4 5 6]) => (2 4 6) ˆ Reduce: Most general- Make something new out of the elements in a collection ˆ (reduce + [1 2 3 4 5 6]) => 21 ˆ (reduce max [3 2 6 4 1 7 5]) => 7 11