Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@the-frey
Created November 24, 2017 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-frey/c673c5b952f085eb9c3023c5ee17c84b to your computer and use it in GitHub Desktop.
Save the-frey/c673c5b952f085eb9c3023c5ee17c84b to your computer and use it in GitHub Desktop.
First Coop Digital Clojure Dojo "Lesson"
;; define something
(def foo 1)
;; define a function
(def hello-world
(fn []
(println "Hello world")))
;; define a function using terse anonymous function syntax
(def hello-2
#(println "Hello world"))
;; define a function using defn syntax
(defn hello-3 []
(println "Hello world"))
;; and another
(defn incrementor [number]
(+ number 1))
;; if your function takes one argument, reger to it otherwise it will
;; need to be in an anonymous function
(map #(clojure.string/replace % #" " "") ["foo" "bar" "baz " "jeff vader"])
(map inc [1 2 3 4 5 5])
(map nil? [1 2 3])
;; {:name "Alex" :surname "Lynham"}
(defn local-state-example []
(let [customer-first-name "Alex"
customer-last-name "Lynham"
customer-full-name (str customer-first-name
" "
customer-last-name)]
(println customer-full-name)))
(local-state-example)
(defn second-local-state-example [{first-name :first-name
last-name :last-name}]
(let [customer-full-name (str first-name
" "
last-name)]
(println customer-full-name)))
;; the above example without destructuring
(defn third-local-state-example [input-hash]
(let [first-name (:first-name input-hash)
last-name (:last-name input-hash)
customer-full-name (str first-name
" "
last-name)]
(println customer-full-name)))
(second-local-state-example {:first-name "alex" :last-name "lynham"})
(third-local-state-example {:first-name "jeff" :last-name "vader"})
(defn implicit-return []
1)
;; seq
'(1 2 3 4 5)
[1 2 3 4 5]
#{1 2 3 4 5}
(def vector-with-duplicates [1 2 3 4 4 5])
(def vector-with-nils [1 2 3 4 nil 5 6 7])
;; transform into a set
(into #{} vector-with-duplicates)
;; get nth element of a list
(nth vector-with-duplicates 3)
;; filter out nils
(filter identity vector-with-nils)
{:foo "bar"
:baz "some other damn text"}
;; https://clojure.org/reference/data_structures#Maps for more info
;; hash access
(def a-hash-map {:key-i-want-to-access "this was a bad example and I regret that now"})
(a-hash-map :key-i-want-to-access)
(:key-i-want-to-access a-hash-map)
;; lazy-seqs
(def my-first-lazy-seq (lazy-seq [1 2 3 4 5 6 6 7 7 45 4 54 45 54 45 45 57 73 46 634 36 634 346 634 6 43 634 436 634 436 643 634 364 63 364 436 346 34 6]))
;; this is purely to generate an infinite seq - don't worry about the
;; implementation yet!
(defn positive-numbers
([] (positive-numbers 1))
([n] (lazy-seq (cons n (positive-numbers (inc n))))))
;; don't eval this!! It's an infinite lazy seq!!
;; I did because I'm an idiot
;; (positive-numbers)
(take 10 (positive-numbers))
;; don't do this, it will eval the seq and crash!
;; (count (positive-numbers))
;; the big kahuna
;; threading macros motherf***er
(def a-rubbish-string "%% here's some target text ")
(def some-data [1 2 3 4])
;; thread first
;; the first argument is passed as the first argument to every
;; function in the list that follows
;; generally used for scalar values
(-> a-rubbish-string
(clojure.string/replace #"%" "")
clojure.string/trim
clojure.string/capitalize)
;; thread last
;; the first argument is passed as the last argument to every function
;; in the list that follows
;; generally used for collections
(->> some-data
(map inc)
(map str)
(map #(Integer/parseInt %)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment