Skip to content

Instantly share code, notes, and snippets.

@theronic
Created December 27, 2021 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theronic/b95688e78e731c35f6b8ef9b8c13d076 to your computer and use it in GitHub Desktop.
Save theronic/b95688e78e731c35f6b8ef9b8c13d076 to your computer and use it in GitHub Desktop.
Teaching Basic Clojure
(ns luandro)
(even? 3)
(apply + (filter even? (range 20)))
(->> (range 20)
(filter even?)
(apply +))
(macroexpand '(-> person (get :name)))
(apply + (filter even? (range 20)))
;(def person {:name "Leandro"
; :address {:city "Johannesburg"
; :street "Rooivink"}
; :age 16})
;
;(defn greeting [person]
; (str "Hi " (:name person) "! Welcome to " (:location person)))
;
;(greeting (assoc person :location "Boggomsbaai"))
(def test-line
{:name "Milk"
:qty 15
:discount 30M
:unit-price 23.0M})
;345 + 10M = 355M
(def test-line2
{:name "Bread"
:qty 2
:unit-price 5.0M})
(def order
{:number "SO-1234"
:lines [test-line test-line2]})
(defn calc-line-total
[{:as line
:keys [qty unit-price discount]
:or {discount 0M}}]
(- (* qty unit-price) discount))
(defn calc-total [{:as order, :keys [lines]}]
(->> lines
(map calc-line-total)
(apply +)))
(defn calc-tax [order]
(* 0.15) 780M)
(defn process-order [order]
(assoc order
:total (calc-total order)
:tax (calc-tax order)))
(comment
(process-order order)
(foo order)
(calc-line-total (assoc test-line :discount 15M))
(calc-line-total test-line2)
(->> [test-line test-line2]
(map calc-line-total)
(apply +))
(- 5 nil)
(calc-total order)
(assoc person :location "Boggomsbaai")
;; Operational Form:
;(operator op1 op2 op3 ...)
(def my-name "Leandro")
(str "Hi " (:name person "Petrus") "!")
(eval (cons 'clojure.core/+ (cons 6 (rest '(+ 1 2 3)))))
'(+ 1 2 3)
(eval '(+ 1 2 3))
(def foo (fn [x] x))
(macroexpand '(defn foo [x] x))
(foo 3456)
"Hello world"
:age :name
(and true true false)
(or true false)
(count '[if fn def let loop do while recur . try monitor-enter monitor-exit throw catch]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment