clojure-demos/clojure-basics/src/core.cljc

110 lines
1.4 KiB
Clojure

(ns demo.core)
'(1 2 3)
[1 2 3 4]
{:key-one 1 :key-two 2}
#{1 2 3}
(+ 1 2 3)
(* 2 (+ 1 2 3))
(def my-string "A string once set does not change")
(def my-number 3.14)
(def my-atom-string (atom "A string once set does not change"))
(def my-atom-number (atom 3.14))
(def my-atom-hashmap (atom {:one 1 :two 2}))
@my-atom-string
@my-atom-number
@my-atom-hashmap
(reset! my-atom-string "My new string")
(swap! my-atom-string str " appended text")
(reset! my-atom-hashmap {})
(swap! my-atom-hashmap assoc :key-one "value")
(defonce my-atom-hashmap (atom {:ex-one "hi"}))
(swap! my-atom-hashmap assoc :key-one "value")
(or nil 1)
(and nil 1)
(not true)
(if (true? (not true))
1
2)
(when (= 1 1)
(prn "True so print me!")
(prn "multiple statements aloowed in a when, only last returns a value"))
(if (true? (not true))
(do
(prn "first thing todo")
(prn "second thing todo, last statement will be the return")
1)
2)
(loop [n 0]
(if (< n 5)
(recur (inc n))
n))
(for [x [1 2 3 4 5 6]]
(* x x))
(def my-hashmap {:top {:first-key 1 :second-key 2}})
(get (get my-hashmap :top) :second-key)
(get-in my-hashmap [:top :second-key])
(:second-key (:top my-hashmap))
(-> my-hashmap :top :second-key)
(defn my-fn [{:keys [id name value missing] :or {missing "not set"} :as my-hashmap}]
[id name value missing my-hashmap])
(my-fn {:id 1 :name "bob" :value 456})