Skip to content

Instantly share code, notes, and snippets.

@bsnux
Last active March 4, 2020 01:15
Show Gist options
  • Save bsnux/c8597369a5eb3d210e1e33eca25ae15d to your computer and use it in GitHub Desktop.
Save bsnux/c8597369a5eb3d210e1e33eca25ae15d to your computer and use it in GitHub Desktop.
Simple Clojure introduction
;;
;; Clojure is a Lisp family functional programming language developed for the JVM
;;
;; It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.
;; —Alan Perlis
;;
;; Reference: https://www.braveclojure.com/
;;
; The first call in a file should be ns, to set the namespace
(ns bsnux.basics)
; Binding a name to a value
(def yourname "Lucas")
; No string interpolation but `str` allows us to concat strings
(str "Hello " yourname)
; Basic data structures: numbers, strings, maps, keywords, vectors, list, sets
(def x 5.5)
(def y 4.0)
(+ x y)
; Maps
(def mymap {:first-name "Tom" :last-name "Sawyer"})
(get mymap :first-name)
(get mymap :age "empty")
; Lists and vectors are similar. If you need to easily add items to the beginning
; of a sequence or if you’re writing a macro, you should use a list. Otherwise, you should use a vector
; Using `nth` to retrieve an element from a list is slower than using `get` from a vector. Clojure has to traverse
; all n elements of a list to get to the `nth`
; Lists
(def mylist '(1 2 3 4))
(nth mylist 0)
; Vectors
(def myvector [1 3 5])
(conj myvector 6)
(get myvector 0)
; Sets are collections of unique values
(def myset #{"Tom" :age 23})
(set [1 3 4 4 4 4 4])
(contains? myset "Bob")
; Functions
; `map` creates a new list by applying a function to each member of a collection
(map inc [0 1 2 3])
; => (1 2 3 4)
(defn hello-world
"This is a docstring for our hello-world function"
[name]
(str "Hello " name "!"))
(hello-world "Tom")
; => "Hello Tom!"
(defn no-params-fun
"No params here!"
[]
"This is a func with no params")
(no-params-fun)
; => "This is a func with no params"
; Default arguments
(defn test-arity
"Describe the kind of chop you're inflicting on someone"
([first-name last-name]
(str "Hi " first-name " " last-name))
([first-name]
(test-arity first-name "no last name")))
(test-arity "Tom")
; => "Hi Tom no last name"
(test-arity "Tom" "Sawyer")
; => "Hi Tom Sawyer"
; Variable-arity functions by including a rest parameter, as in "put the rest of these arguments
; in a list with the following name". The rest parameter is indicated by an `&`
(defn hello-stranger
[name]
(str "Hello stranger, " name "!!!"))
(defn hello-you-all
[& names]
(map hello-stranger names))
(hello-you-all "Tom" "Bob" "Alice")
; => ("Hello stranger, Tom!!!" "Hello stranger, Bob!!!" "Hello stranger, Alice!!!")
;; Anonymous functions
(map (fn [name] (str "Hi, " name)) ["Tom" "Bob" "Alice"])
;; Syntatic sugar for anonymous functions
(map #(str "Hi, " %) ["Tom" "Bob" "Alice"])
;; Returning functions
(defn inc-maker
"Create a custom incrementor"
[inc-by]
#(+ % inc-by))
(def inc2 (inc-maker 2))
(inc2 8)
; => 10
(def inc4 (inc-maker 4))
(inc4 8)
; => 12
;; Regular expressions
(re-find #"^left-" "left-eye")
; => "left-"
(re-find #"^left-$" "left-eye")
; => nil
(re-matches #"abc(.*)" "abcxyz")
; => ["abcxyz" "xyz"]
(re-matches #"abc" "zzzabcxxx")
; => nil
(re-find #"s+" "dress")
; => "ss"
(re-find #"s+(.*)(s+)" "success")
; => ["success" "ucces" "s"]
; GET request
(slurp "https://www.braveclojure.com/random-quote")
; Parsing GET JSON response
(require '[clojure.data.json :as json])
(json/read-str (slurp "https://jsonplaceholder.typicode.com/todos/1"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment