Presentation about clojure core concepts/functions.

presentation.tex 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. \documentclass{beamer}
  2. \title{Clojure Core}
  3. \subtitle{Primatives, collections, functions, and macros}
  4. \author{Lily Carpenter}
  5. \institute{https://gitlab.com/azrazalea/}
  6. \mode<presentation> {\usetheme{Dresden}}
  7. \date{}
  8. \usepackage[latin1]{inputenc}
  9. \usepackage{times}
  10. \usepackage[T1]{fontenc}
  11. \usepackage[english]{babel}
  12. \usepackage{hyperref}
  13. \usepackage{minted}
  14. \usepackage{upquote}
  15. \AtBeginDocument{%
  16. \def\PYZsq{\textquotesingle}%
  17. }
  18. \begin{document}
  19. \begin{frame}
  20. \titlepage
  21. \end{frame}
  22. \begin{frame}
  23. \frametitle{Summary}
  24. \tableofcontents
  25. \end{frame}
  26. \section{Primitives}
  27. \begin{frame}
  28. \frametitle{Primitives}
  29. Uses host language primitives
  30. \begin{itemize}
  31. \item{Integers}
  32. \item{Floating points}
  33. \item{Ratios}
  34. \item{Strings}
  35. \item{Regexes}
  36. \item{Keywords}
  37. \item{Symbols}
  38. \end{itemize}
  39. \end{frame}
  40. \begin{frame}
  41. \frametitle{Integers}
  42. \begin{itemize}
  43. \item{Standard java Long}
  44. \item{Automatically upgrade to arbitrary precision BigInteger with certain functions}
  45. \item{BigInteger does not exist for ClojureScript}
  46. \end{itemize}
  47. \end{frame}
  48. \begin{frame}[fragile]
  49. \frametitle{Integers}
  50. \begin{minted}{clojure}
  51. (+ 1 1)
  52. ;=> 2
  53. (/ 6 3)
  54. ;=> 2
  55. (> 3 2 1)
  56. ;=> false
  57. (< 3 2 1)
  58. ;=> true
  59. (class (+ 1 1))
  60. ;=> java.lang.Long
  61. \end{minted}
  62. \end{frame}
  63. \begin{frame}[fragile]
  64. \frametitle{Integers}
  65. \begin{minted}{clojure}
  66. ;; To the more experienced,
  67. ;; why does this not need +'?
  68. (class (+ 200000000000000000000
  69. 100000000000000000))
  70. ;=> clojure.lang.BigInt
  71. \end{minted}
  72. \end{frame}
  73. \begin{frame}
  74. \frametitle{Floating points}
  75. \begin{itemize}
  76. \item{Standard java Double}
  77. \item{Automatically upgrade to BigDecimal with certain functions}
  78. \item{BigDecimal does not exist for ClojureScript}
  79. \end{itemize}
  80. \end{frame}
  81. \begin{frame}[fragile]
  82. \frametitle{Floating points}
  83. \begin{minted}{clojure}
  84. (+ 0.1 0.1) ; leading zero required
  85. ;=> 0.2
  86. (* 0.1 0.1)
  87. ;=> 0.010000000000000002
  88. (class (* 0.1 0.1))
  89. ;=> java.lang.Double
  90. ;; Note the *', needed to promote doubles
  91. (class (*' (- 2M 2e-52M) 2e1023M))
  92. ;=> java.math.BigDecimal
  93. \end{minted}
  94. \end{frame}
  95. \begin{frame}
  96. \frametitle{Ratios}
  97. \begin{itemize}
  98. \item{Represents fractions without using floating point}
  99. \item{Standard numeric operations work}
  100. \item{Automatically reduces}
  101. \item{Not in clojurescript}
  102. \end{itemize}
  103. \end{frame}
  104. \begin{frame}[fragile]
  105. \frametitle{Ratios}
  106. \begin{minted}{clojure}
  107. (/ 2 3)
  108. ;=> 2/3
  109. (/ 3 6)
  110. ;=> 1/2
  111. (+ 1/2 1/2)
  112. ;=> 1N
  113. (class (+ 1/2 1/2))
  114. ;=> clojure.lang.BigInt
  115. (+ 3/4 0.25)
  116. ;=> 1.0
  117. (class (+ 3/4 0.25))
  118. ;=> java.lang.Double
  119. (+ 3/4 1)
  120. ;=> 7/4
  121. \end{minted}
  122. \end{frame}
  123. \begin{frame}
  124. \frametitle{Strings}
  125. \begin{itemize}
  126. \item{Java strings}
  127. \item{Common to use Java methods to manipulate}
  128. \end{itemize}
  129. \end{frame}
  130. \begin{frame}[fragile]
  131. \frametitle{Strings}
  132. \begin{minted}{clojure}
  133. (.toUpperCase "blah")
  134. ;=> "BLAH"
  135. (.split "BLAH BLAH BLAH" "")
  136. ;=> ("BLAH" "BLAH" "BLAH")
  137. (clojure.string/upper-case "blah")
  138. ;=> "BLAH"
  139. (clojure.string/split "BLAH BLAH BLAH" "")
  140. ;=> ("BLAH" "BLAH" "BLAH")
  141. \end{minted}
  142. \end{frame}
  143. \begin{frame}
  144. \frametitle{Regexes}
  145. \begin{itemize}
  146. \item{Use either re-pattern or literal syntax}
  147. \item{Creates java.util.regex.Pattern}
  148. \item{Use re-seq, re-find, re-matches}
  149. \end{itemize}
  150. \end{frame}
  151. \begin{frame}[fragile]
  152. \frametitle{Regex}
  153. \begin{minted}{clojure}
  154. (re-seq #"[0-9]+" "abs123def345ghi567")
  155. ;=> ("123" "345" "567")
  156. (re-find #"([-+]?[0-9]+)/([0-9]+)" "22/7")
  157. ;=> ["22/7" "22" "7"]
  158. \end{minted}
  159. \end{frame}
  160. \begin{frame}
  161. \frametitle{Keywords}
  162. \begin{itemize}
  163. \item{Identifiers that just evaluate to themselves}
  164. \item{Can be used to lookup keys in maps}
  165. \end{itemize}
  166. \end{frame}
  167. \begin{frame}[fragile]
  168. \frametitle{Keywords}
  169. \begin{minted}{clojure}
  170. :foo
  171. ;=> :foo
  172. (keyword 'foo)
  173. ;=> :foo
  174. (:foo {:foo 1})
  175. ;=> 1
  176. \end{minted}
  177. \end{frame}
  178. \begin{frame}
  179. \frametitle{Symbols}
  180. \begin{itemize}
  181. \item{"Variables": Not really, but partially serve the same purpose}
  182. \item{They are identifiers pointing to something else}
  183. \item{They don't vary (hence not really variables)}
  184. \item{The actual storage location is called a var, symbol is just a name that points to it}
  185. \end{itemize}
  186. \end{frame}
  187. \begin{frame}[fragile]
  188. \frametitle{Symbols}
  189. \begin{minted}{clojure}
  190. (symbol 'foo)
  191. ;=> foo
  192. (symbol "foo")
  193. ;=> foo
  194. (symbol "clojure.core" "foo")
  195. ;=> clojure.core/foo
  196. (defn blah
  197. []
  198. (println "blah"))
  199. ;=> #'user/blah
  200. (let [x 5]
  201. (+ x 2))
  202. ;=> 7
  203. \end{minted}
  204. \end{frame}
  205. \section{Sequences}
  206. \begin{frame}
  207. \frametitle{Sequences}
  208. \begin{itemize}
  209. \item{Generalized interface implemented by most collections.}
  210. \item{Many functions on sequences are lazy}
  211. \item{first}
  212. \item{rest}
  213. \item{cons}
  214. \end{itemize}
  215. \end{frame}
  216. \section{Collections}
  217. \begin{frame}
  218. All persistent(shared structure). Also implement the ISeq interface.
  219. \frametitle{Collections}
  220. \begin{itemize}
  221. \item{Lists}
  222. \item{Vectors}
  223. \item{Maps}
  224. \item{Sets}
  225. \item{Records}
  226. \end{itemize}
  227. \end{frame}
  228. \begin{frame}
  229. \frametitle{Lists}
  230. \begin{itemize}
  231. \item{Basic linked list in function}
  232. \item{Directly implements Sequence interface, except for empty list}
  233. \item{Conj puts the item at the front of the list}
  234. \end{itemize}
  235. \end{frame}
  236. \begin{frame}[fragile]
  237. \frametitle{Lists}
  238. \begin{minted}{clojure}
  239. '(1 2 3 4) ; Literal list
  240. ;=> (1 2 3 4)
  241. (list 1 2 3 4)
  242. ;=> (1 2 3 4)
  243. (+ 1 2) ; List as code structure
  244. ;=> 3
  245. (first '(1 2 3 4)) ; Using list as data structure
  246. ;=> 1
  247. (rest '(1 2 3 4))
  248. ;=> (2 3 4)
  249. (conj '(1 2 3 4) 5)
  250. ;=> (5 1 2 3 4)
  251. \end{minted}
  252. \end{frame}
  253. \begin{frame}
  254. \frametitle{Vectors}
  255. \begin{itemize}
  256. \item{Very similar to Arrays in other languages}
  257. \item{Conj puts the item at the end of the vector}
  258. \item{Supports rseq function for quick reversal}
  259. \end{itemize}
  260. \end{frame}
  261. \begin{frame}[fragile]
  262. \frametitle{Vectors}
  263. \begin{minted}{clojure}
  264. [1 2 3 4 5] ; Literal vector
  265. ;=> [1 2 3 4 5]
  266. (vector 1 2 3 4 5)
  267. ;=> [1 2 3 4 5]
  268. (conj [1 2 3 4] 5)
  269. ;=> [1 2 3 4 5]
  270. (rseq [1 2 3 4 5])
  271. ;=> (5 4 3 2 1)
  272. \end{minted}
  273. \end{frame}
  274. \begin{frame}
  275. \frametitle{Maps}
  276. \begin{itemize}
  277. \item{Standard key and value pairs}
  278. \item{Either hashed or sorted}
  279. \item{Conj expects another Map and merges them}
  280. \end{itemize}
  281. \end{frame}
  282. \begin{frame}[fragile]
  283. \frametitle{Maps}
  284. \begin{minted}{clojure}
  285. {:key1 1, :key1 2} ; Literal HashMap
  286. ;=> {:key1 2}
  287. (hash-map :key1 1, :key1 2)
  288. ;=> {:key1 2}
  289. (assoc {} :key1 1)
  290. ;=> {:key1 1}
  291. (dissoc {:key1 1} :key1)
  292. ;=> {}
  293. (conj {:key1 1} {:key2 2})
  294. ;=> {:key1 1 :key2 2}
  295. \end{minted}
  296. \end{frame}
  297. \begin{frame}
  298. \frametitle{Sets}
  299. \begin{itemize}
  300. \item{Unique set of values}
  301. \item{Either hashed or sorted}
  302. \item{Conj adds new elements to set}
  303. \item{Various set operations including union, difference, and intersection.}
  304. \end{itemize}
  305. \end{frame}
  306. \begin{frame}[fragile]
  307. \frametitle{Sets}
  308. \begin{minted}{clojure}
  309. #{1 2 3} ; Literal HashSet
  310. ;=> #{1 2 3}
  311. (hash-set 1 2 3)
  312. ;=> #{1 2 3}
  313. (conj #{1 2 3} 4)
  314. ;=> #{1 2 3 4}
  315. (disj #{1 2 3} 3)
  316. ;=> #{1 2}
  317. \end{minted}
  318. \end{frame}
  319. \begin{frame}
  320. \frametitle{Basic collection functions}
  321. \begin{itemize}
  322. \item{Reduce: Standard functional reduce(fold left), iterate over collection and return a single accumulated result.}
  323. \item{Map: Standard functional map, iterate over collection and return a Sequence containing one value per a collection item.}
  324. \end{itemize}
  325. \end{frame}
  326. \begin{frame}[fragile]
  327. \frametitle{Basic collection functions}
  328. \begin{minted}{clojure}
  329. (reduce + [1 2 3 4 5])
  330. ;=> 15
  331. (reduce conj #{} [:a :b :c])
  332. ;=> #{:a :b :c}
  333. (map inc [1 2 3])
  334. ;=> (2 3 4)
  335. (map + [1 2 3] [4 5 6] [7 8 9])
  336. ;=> (12 15 18)
  337. (defn wrong-arity-func
  338. [a b]
  339. (+ a b))
  340. (map wrong-arity-func [1 2 3] [4 5 6] [7 8 9])
  341. ;=> ArityException: Wrong number of args (3)
  342. ;=> passed to wrong-arity-func
  343. \end{minted}
  344. \end{frame}
  345. \section{Control Flow}
  346. \begin{frame}
  347. \frametitle{Looping/recursion}
  348. \begin{itemize}
  349. \item{Loop: Provides a lexical closure to recur to.}
  350. \item{Recur: Used for loop control flow as well as for general recursive functions.}
  351. \item{Recur will throw an exception if it is not in tail position}
  352. \item{Quite often your use case can be solved using map/reduce or similar instead.}
  353. \end{itemize}
  354. \end{frame}
  355. \begin{frame}[fragile]
  356. \frametitle{Looping/recursion}
  357. \begin{minted}{clojure}
  358. (loop [x 10]
  359. (when (> x 1)
  360. (println x)
  361. (recur (- x 2))))
  362. ;=> 10
  363. ;=> 2
  364. (defn count-to-10 [x]
  365. (when (<= x 10)
  366. (println x)
  367. (recur (inc x))))
  368. (count-to-10 5)
  369. ;=> 5
  370. ;=> 10
  371. \end{minted}
  372. \end{frame}
  373. \begin{frame}[fragile]
  374. \frametitle{Looping/recursion}
  375. \begin{minted}{clojure}
  376. (defn count-to-10 [x]
  377. (when (<= x 10)
  378. (recur (inc x))
  379. (println x)))
  380. ;=> CompilerException java.lang
  381. ;=> .UnsupportedOperationException: Can only
  382. ;=> recur from tail position
  383. \end{minted}
  384. \end{frame}
  385. \begin{frame}
  386. \frametitle{Conditionals}
  387. \begin{itemize}
  388. \item{If: Basic if statement, only support true and false branches for one statement. }
  389. \item{Cond: More complex if, takes many different conditionals.}
  390. \item{Case: Standard switch case statement}
  391. \end{itemize}
  392. \end{frame}
  393. \begin{frame}[fragile]
  394. \frametitle{Conditionals}
  395. \begin{minted}{clojure}
  396. (if (even? 6)
  397. (println true)
  398. (println false))
  399. ;=> true
  400. (if (even? 5)
  401. (println true)
  402. (println false))
  403. ;=> false
  404. (cond
  405. (< 5 0) true
  406. (> 5 0) false
  407. :else nil)
  408. ;=> false
  409. \end{minted}
  410. \end{frame}
  411. \begin{frame}[fragile]
  412. \frametitle{Conditionals}
  413. \begin{minted}{clojure}
  414. (cond
  415. (< 0 5) true
  416. (> 0 5) false
  417. :else nil)
  418. ;=> true
  419. (let [mystr "no match"]
  420. (case mystr
  421. "" 0
  422. "hello" (count mystr)
  423. "default"))
  424. ;=> "default"
  425. \end{minted}
  426. \end{frame}
  427. \section{Macros}
  428. \begin{frame}
  429. \frametitle{Macros}
  430. Very very very brief(and technically incorrect) explanation is: \\~\\
  431. A 'function' that does not evaluate its arguments by default and returns
  432. a list that will then be evaluated as a code form. \\~\\
  433. Read \url{http://www.braveclojure.com/writing-macros/}
  434. \end{frame}
  435. \begin{frame}[fragile]
  436. \frametitle{Macros}
  437. \begin{minted}{clojure}
  438. (defmacro pointless
  439. [n]
  440. n)
  441. (pointless (+ 1 2))
  442. ;=> 3
  443. (pointless 3)
  444. ;=> 3
  445. (pointless [1 2 3 4])
  446. ;=> [1 2 3 4]
  447. \end{minted}
  448. \end{frame}
  449. \begin{frame}
  450. \begin{center}
  451. \Huge Questions?
  452. \end{center}
  453. \end{frame}
  454. \end{document}