Short Tour Of Clojure

Short Tour Of Clojure

C6dbbb399f7658917f60bd1e2a2663ed?s=128

Tamizhvendan S

May 23, 2020
Tweet

Transcript

  1. Lead Consultant www.ajira.tech Tamizhvendan S Passionate, Pragmatic and Polyglot Programmer

    https://www.demystifyfp.com tamizhvendan (short-tour (of (clojure)))
  2. Credits: https://twitter.com/andreasdotorg/status/570190592610832384

  3. None
  4. None
  5. Credits: https://github.com/tallesl/Rich-Hickey-fanclub Simple Made Easy Hammock Driven Development The Value

    of Values Effective Programs
  6. Credits: https://www.artsy.net/artwork/brian-kernighan-hello-world

  7. Hello World in three languages printf("Hello, World!\n"); C System.out.println("Hello, World");

    Java println("Hello, World") Scala
  8. Hello World Requirements 1. A String Representation 2. Function/Method

  9. function (argument) Function/Method Invocation

  10. function (argument) Clojure’s way! (function argument)

  11. Hello World in Clojure (println "Hello, World!")

  12. (println "Hello, World!") function argument whitespace Clojure Syntax

  13. Functions with multiple arguments printf("hello, %s\n", "World!"); C System.out.printf("Hello, %s\n",

    "World!"); Java printf("Hello, %s\n", "World!") Scala
  14. function (argument1, argument2) Function/Method Invocation

  15. (function argument1, argument2) Clojure’s way! function (argument1, argument2)

  16. (function argument1, argument2) (function argument1 argument2) optional Clojure’s way!

  17. (function argument1 argument2 argument3 …) Clojure’s way!

  18. Consistent Syntax 1. Adding the numbers one, two, three &

    four 2. Converting an integer to a string 3. Concatenating two strings 4. Conditional Operator
  19. Adding the numbers one, two, three & four 1 +

    2 + 3 + 4 Converting an integer to a string String.valueOf(1) Concatenating two strings "Hello, ".concat("World!")
  20. 1 + 2 + 3 + 4 function function function

    argument1 argument2 argument3 argument4 Infix Notation
  21. method String.valueOf(1) ClassName argument invoke function Prefix Notation

  22. method "Hello, ".concat("World!") Object argument invoke function Prefix Notation

  23. argument2 true ? "truthy" : "falsy" argument1 separator function argument3

    Conditional Operator
  24. function argument2 argument4 argument1 argument3 (+ 1 2 3 4)

    Clojure Syntax 1 + 2 + 3 + 4
  25. (String/valueOf 1) function argument1 class name static method argument(s) to

    the static method Clojure Syntax String.valueOf(1)
  26. (. "Hello, " concat "World!") function argument1 argument2 argument3 instance

    instance method argument(s) to the instance method special form Clojure Syntax "Hello, ".concat("World!")
  27. (if true "truthy" "falsy") function argument1 argument2 argument3 boolean value

    value1 value2 special form Clojure Syntax true ? "truthy" : "falsy"
  28. Clojure Syntax

  29. Clojure Form A structure that the Clojure compiler recognise.

  30. Types of Clojure Forms Atomic Data Type - Literals "Hello,

    World" 1 1.2 true nil \a :keyword symbol false
  31. Types of Clojure Forms Collection - Literals [1 "2" 3]

    '(1 1.2 1.5) {:one 1 :two 2 :three 3} #{1 2 3}
  32. Types of Clojure Forms Expressions (function invocation) (println "Hello, World!")

    (println "Hello," "World!") (+ 1 2 3 4) (String/valueOf 1) (. "Hello, " concat "World!")
  33. Literals evaluate to itself user!=> "Hello, World" "Hello, World" user!=>

    1 1
  34. Expression always return a value user!=> (println "Hello, Clojure!") Hello,

    Clojure! nil  output of println function  return value of println function
  35. Forms can be nested (println "1 + 1 =" (+

    1 1))
  36. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) left to right
  37. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) An expression begins with “(“
  38. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) function symbol println is a variadic function defined in the clojure.core namespace.
  39. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) argument1 string literal literals evaluate to itself
  40. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) An expression begins with “(“ argument2
  41. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) function symbol + is a variadic function defined in the clojure.core namespace.
  42. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) argument1 integer literal literals evaluate to itself
  43. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) argument2 integer literal literals evaluate to itself
  44. Clojure Evaluation Model (println "1 + 1 =" (+ 1

    1)) An expression ends with “)“
  45. Clojure Evaluation Model (println "1 + 1 =" 2) All

    expressions return a value
  46. Clojure Evaluation Model (println "1 + 1 =" 2) An

    expression ends with “)“
  47. Clojure Evaluation Model (println "1 + 1 =" 2) user!=>

    (println "1 + 1 =" (+ 1 1)) 1 + 1 = 2 nil  output of println function  return value of println function
  48. Clojure Evaluation Model Deviations Special Forms if . def let

    quote var fn loop recur Macros when condp -> ->> Macros are programs that write programs! try do
  49. Summary f(x) translates to (f x) f(x, y) translates to

    (f x y) o.m(x) translates to (. o m x) 1 + 2 + 3 + 4 translates to (+ 1 2 3 4) Prefix Notation
  50. Demo!

  51. Let’s start!