Skip to content

Instantly share code, notes, and snippets.

@craftybones
Last active October 23, 2016 09:15
Show Gist options
  • Save craftybones/7be806de89aacd1098979b559dee61e7 to your computer and use it in GitHub Desktop.
Save craftybones/7be806de89aacd1098979b559dee61e7 to your computer and use it in GitHub Desktop.
; first argument to list is always a function
(println "Hello World")
; + - * and / are functions!
(+ 2 3)
(- 2 3)
(* 3 4)
(/ 10 5)
; They can take multiple arguments
(+ 2 3 4) ; etc
; you may bind with def
(def foo 5)
(def name "Jayanth")
(def bignum 10000000000N)
; functions are defined with fn
(def sqr (fn [x] (* x x)))
; but cleaner to define with special form defn
(defn sqr [x] (* x x))
; if is a function too. Sort of.
(if (> 5 4)
"5 is greater than 4"
"4 is greater than 5")
; These are lists
; Note the ' in front of the (). Why?
; Lists are linked lists
; O(1) on first/last, O(n) on indexed access
(def scores '(100 20 80))
; These are vectors
; Lists are linked lists
; O(1) on first/last, O(1) on indexed access?
(def scores [100 20 80])
; hash-maps
; access is O(1). Obviously :D
; keys can be quite complex.
; Does not convert to string unlike js
; values can be anything
(def person {:name "Jayanth"
:age 100
:likes ["green" "guitar" "music" "art"]
:dislikes ["yellow" "tuba"]})
(person :name)
(person :age)
(get-in person [:likes 1])
; sets
; sets are a first class data structure in Clojure
(hash-set 1 2 3)
; sequences
(def scores [10 13 15 50 93 98])
(first scores)
(rest scores)
(cons 100 scores)
; map filter reduce on vectors
(def scores [10 13 15 50 93 98])
(map inc scores)
(filter odd? scores)
(reduce + scores)
; map filter reduce on lists
(def scores '(10 13 15 50 93 98))
(map inc scores)
(filter odd? scores)
(reduce + scores)
; partials and composing functions
(def vowels #{\a \e \i \o \u})
(defn is-vowel? [x]
(contains? vowels x))
(is-vowel? \a)
(is-vowel? \b)
; using a partial instead
(def vowels #{\a \e \i \o \u})
(defn is-vowel? (partial contains? vowels))
(is-vowel? \a)
(is-vowel? \b)
; composing functions
(def vowels #{\a \e \i \o \u})
(defn is-vowel? (partial contains? vowels))
(defn is-consonant? [x]
(not (is-vowel? x)))
(is-consonant? \a)
(is-consonant? \b)
(def vowels #{\a \e \i \o \u})
(defn is-vowel? (partial contains? vowels))
(def is-consonant? (comp not is-vowel?))
(is-consonant? \a)
(is-consonant? \b)
; (comp not..) is so common that Clojure has a complement function that achieves the same thing
(def vowels #{\a \e \i \o \u})
(defn is-vowel? (partial contains? vowels))
(def is-consonant? (complement is-vowel?))
(is-consonant? \a)
(is-consonant? \b)
; macros
; Clojure code is held in clojure lists
; homoiconicity
'(+ 1 2) ; is a valid list
(eval '(+ 1 2))
; our unless
(defmacro unless [pred a b]
`(if (not ~pred) ~a ~b))
; macros are useful when evaluation
; can't happen before fn call
; Both println will be called regardless
; and before unless is called even
(defn unless [pred a b]
(if (not pred) a b))
(unless false
(println "This is a joke")
(println "This is not a joke"))
; or in the case of defn
(macroexpand '(defn foo [x] (inc x)))
; java interop
(type "hello")
(.toUpperCase "hello")
; add customary Swing example here
; Add notes on
; * Destructuring
; * Namespaces
; * Docstring
; * binding forms (let, do etc)
; * Recursion
; * Loops
; * Conditionals(when, case etc)
; * Polymorphism (defstruct, defrecord, defmulti etc)
; * transducers
; * Atoms/Refs/Vars/Agents/Futures concurrency
; * Immutability
; * Common libraries
; * use, require
; * lein/boot
; * Unit testing midje etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment