Skip to content

Instantly share code, notes, and snippets.

@NguyenDa18
Last active July 3, 2018 22:32
Show Gist options
  • Select an option

  • Save NguyenDa18/37d6b912e00a40fe298666aac5d18478 to your computer and use it in GitHub Desktop.

Select an option

Save NguyenDa18/37d6b912e00a40fe298666aac5d18478 to your computer and use it in GitHub Desktop.
Clojure Crash Course

Clojure Basics Tutorial - Learning for Cambia ADIF Team

Package file into JAR with Leinengen

  • lein uberjar
  • Execute with: java -jar target/uberjar/<your-code.jar>

Clojure recognizes two kinds of structures:

  1. Literal representations of data structs
  2. Operations

Variables: immutable and assigned types

  • (def randVar 10) : declare a long
  • (def aDouble 1.233) : double
  • (type false) | (type true) : Bool
  • (nil) : Check no val

Functions: Defined by 5 parts

  1. defn
  2. Function name
  3. Docstring describing function
  4. Parameters in brackets
  5. Function body
(defn -main
  "These are comments"
  [& args]
  (println "Hello World"))

Operations

(str "Hi" "There" "Dude") : Concatenate (pos? 15) : Check positive (even? 26) (odd? 13) (number? num)

Control Flow: if, do, when

conditionals

(if boolean-form
  then-form
  optional-else-form)

do operator wraps up multiple forms in parens and runs each

(if true
 (do (println "Do first")
             "Then this")
 (do (println "Else...")
           ("Do these"))

Data Structures

Maps

{:first-name "Charlie"
 :last-name "MacDennis"}

Hash-Maps : Another way to declare

(hash-map :a 1 :b 2)

(get {:a 0 :b 1} :b)
; => 1

Vectors

[3 2 1]
(vector 3 2 1)

Lists

'(1 2 3 4)

Sets: Hash sets and Sorted sets

#{"kurt" 30 :icicle}
(hash-set 1 1 2 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment