Skip to content

Instantly share code, notes, and snippets.

@shaunr0b
Created May 16, 2014 17:12
Show Gist options
  • Save shaunr0b/f18df6ee3fb35e0a8331 to your computer and use it in GitHub Desktop.
Save shaunr0b/f18df6ee3fb35e0a8331 to your computer and use it in GitHub Desktop.
; NUMBERS
(+ 5 5)
(* 5 5)
(/ 5 5)
(- 5 5)
(/ 2 3)
(/ 2.2 3.3)
(rand)
(rand-int 500)
(mod 5 4)
(odd? 5)
(odd? 4)
(even? 4)
; STRINGS
(str "Eric" " " "Koester")
(re-find #"[0-9]+" "1112a3345")
(clojure.string/replace "python is awesome" "python" "clojure")
(clojure.string/capitalize "pleasant pops")
(clojure.string/split "pleasant pops" #" ")
(clojure.string/join ["abc" "123"])
; KEYWORDS
(keyword "main-street-genome")
(name :mainstreetgenome)
;; COLLECTIONS
; LISTS
'("emily" "mike" "shaun" 1 2 3)
(list "emily" "mike" "shaun" 1 2 3)
(conj '(1 2 3) 4)
(conj [1 2 3] 4)
(first '("emily" "mike" "shaun"))
; VECTORS
["emily" "mike" "shaun"]
(get ["emily" "mike" "shaun"] 2)
(nth ["emily" "mike" "shaun"] 2)
(conj ["emily" "mike" "shaun"] "sofia")
(second ["emily" "mike"])
; SETS
#{"Big Buns" "Pleasant Pops"}
; #{"Big Buns" "Pleasant Pops" "Pleasant Pops"}
#{"Tonic" "1905" "Pleasant Pops"}
(clojure.set/union
#{"Big Buns" "Pleasant Pops"}
#{"Tonic" "1905" "Pleasant Pops"})
(clojure.set/difference
#{"1905" "Big Buns" "Pleasant Pops" }
#{"Pleasant Pops"})
; MAPS
[:name :address]
["shaun" "504 cathedral street baltimore, md"]
(zipmap
[:name :address]
["shaun" "504 cathedral street baltimore, md"])
(get
{:name "shaun" :address "504 cathedral street baltimore, md"}
:address)
; VARS
(def names ["emily" "mike" "shaun" "jordan" "sofia" "eric" "ari" "ben" "ben" "tracy" "jon" "john" "alya" "derek" "marcus"])
names
; CONTROL STRUCTURES
(if (> 2 1)
"Two is greater than one... Phew"
"One is greater than two... Panic")
(cond
(> 1 2) "Panic"
(> 2 1) "Phew"
(> 0 1) "Uh oh"
:else "Oh no")
; FUNCTIONS
(+ 1 2 )
( (fn [a b] (+ a b)) 1 2)
((fn [a b] (+ a b))
1 2)
(def add (fn [a b] (+ 1 2)))
(add 1 2)
(defn add-2 [a b] (+ 1 2))
(add-2 1 2)
(def brown-bags
{:brown-bags [{:session-name "GitHub"
:session-date "last friday"
:session-teacher "jordan"}
{:session-name "LightTable"
:session-date "today"
:session-teacher "shaun"}]})
(map
:session-teacher
(get brown-bags :brown-bags))
; HIGHER ORDER FUNCTIONS (HOFs)
; MAP
names
(get names 0)
(clojure.string/capitalize "abc")
(clojure.string/capitalize (get names 1))
(def uppercase-names
(map
clojure.string/capitalize
names))
uppercase-names
; FILTER
(filter even? [1 2 3 4 5])
names
names
(filter
(fn [current-cursor] (> (count current-cursor) 5))
names)
(defn names-longer-than [names length]
(filter
(fn [name]
(> (count name) length))
names))
(names-longer-than names 100)
; CONTROL
(dotimes [n 100]
(println "doing time" n))
(doseq [n (range 100)]
(println "doing time" n))
; TIME
(time (* 5 4 3 2 1))
(time (* (* 5 4 3 2 1) (* 5 4 3 2 1)))
(time (dorun (range 0 100000000)))
(require '[clojure.data.csv :as csv])
; (require '[clojure.java.io :as io])
(csv/read-csv "1,2,3\na,b,c")
(def member-data
(csv/read-csv (slurp "resources/members.csv")))
(first member-data)
(rest member-data)
(zipmap
(first member-data)
(second member-data))
(defn zip-row [row]
(zipmap
(first member-data)
row))
(defn get-field-from-member-data [which-field]
(map
(fn [row] (get row which-field))
(map
zip-row
(rest member-data))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment