Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. Namespace
  2.  
  3. (ns learnclojure)
  4.  
  5. Types
  6.  
  7. (type 1)
  8. (type 1.1)
  9. (type true)
  10. (type "Hello")
  11. (type (keyword "a"))
  12.  
  13. (type (quote a))
  14. (type 'a)
  15. (type (list 1 2 3))
  16. (type (vector 1 2 3))
  17. (nth (vector 1 2 3) 2)
  18.  
  19. (last (list 1 2 3))
  20.  
  21. List
  22. (type (list 1 2 3))
  23. (type (vector 1 2 3))
  24.  
  25. Map
  26. {:a 1 :b 1 :c 1} {:a 1, :b 1, :c 1}
  27. (hash-map :a 1 :b 1 :c 1)
  28.  
  29. Set
  30. {type #{1 2 3}}
  31. (hash-set 1 2 3)
  32.  
  33. =========================================================
  34.  
  35.  
  36. Control Flow
  37.  
  38. //define var with value
  39. (def x "Hello")
  40.  
  41. (if (empty? x)
  42. "x is empty"
  43. "x is not empty")
  44.  
  45. (if (empty? x)
  46. nil
  47. (do
  48. (println "ok")
  49. :ok))
  50.  
  51.  
  52. (if-not (empty? x)
  53. (do
  54. (println "ok")
  55. :ok))
  56.  
  57. (when (not (empty? x)) :ok)
  58.  
  59. (case x
  60. "Goodby": goodbye
  61. "Hi": hello
  62. :nothing)
  63.  
  64. (cond
  65. (= x "Goodbye") :goodbye
  66. (- (reverse x) "olleH") :olleh
  67. :otherwise :nothing)
  68.  
  69.  
  70. ==============================================
  71.  
  72. Functions
  73.  
  74. (def hello (fn [] "Hello"))
  75.  
  76. (hello)
  77.  
  78. #(str "Helo)
  79.  
  80. (defn hello [] "Hello")
  81.  
  82. (defn hello [name] (str "Hello, " name))
  83. (defn hello "Greets a person named <name> with their <title>" [name, title] (str "Hello, " title " " name))
  84.  
  85. (require '[clojure.repl :refer [doc]])
  86. (doc hello)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement