clojure
Table of Contents
you call the library the framework calls you
clojure speed https://tech.redplanetlabs.com/2020/09/02/clojure-faster/
routing https://github.com/metosin/reitit
left off the tutorial here: https://www.braveclojure.com/concurrency/
also deffinitely need more to look into macros
M-x cider-jack-in
connect to leiningen
1 emacs key commandx
emacs cmd | doom cmd | what do |
---|---|---|
C-x C-e | send line to repl | |
C-c M-n M-n | set repl namespace to current file | |
C-c C-k | recomplile current file in repl | |
C-up or C-down | last commands | |
C-c C-d C-d | explain symbol under pointer | |
M-. | spc c d | go to code |
M-, | return from code | |
C-c C-d C-a | search function names | |
- TODO paredit ersearch personal
https://github.com/georgek/paredit-cheatsheet/blob/master/paredit-cheatsheet.pdf
paredit forward slurp needs key command, maybe C-> paredit-forward-slurp-sexp paredit-forward-barf-sexp
1.1 basics
nil
, true
, false
, if
, do, =when
, nil?
,
(nil? 1) ;; false
falsy: nil
, false
truthy: everything else
(or false 4) ; 4 (or 4 false) ; 4 (and 4 5) ; 5
bind instead of assign
- hashmap
{}
(
get MAP key default
) (get-in MAP [:key :path] default)
(MAP :key)
(MAP :key default)
(:key MAP default)
into
=> (def m {:a "jeu"}) => (into m {:b "kk"}) {:a "jeu", :b "kk"}
also see
assoc
(assoc {:a 1} :b 2) ;f{:a 1 :b 2}
- vectors
[]
conj
adds one to the vector end(get VECTOR index)
=> (def z [m]) => (into z #{{:b "kk"}}) [{:a "jeu"} {:b "kk"}]
- lists
'()
no get,
nth
insteadconj
adds to lost beginning - hashed sets
#{}
hash-set
createsconj
(set VECTOR)
(contains? #{} :key)
get
- functions
(defn function-name "doc string" [args] (res))
; multi arity (defn funk ([arg1 arg3] (something)) ([arg1] (something))) (defn funk [arg & rest] (something))
anonymous
(fn [args] (body))
#(* % 3)
arg gets inserted in % further%1 %2 %&
- loop
it’s almost exactly like a function, that allows tail calling
(loop [arg 0] (recur (inc arg))) ; infinite loop. =recur= calls the func
- Sequence functions
filter
,take
,drop
,take-while
,drop-while
,some
,sort
,sortBy
,concat
repeat
,repeatedly
lazy - Collection functions
empty
,every
- Function functions
partial
,apply
,complement
comp
,memoize
- comp vs -> (pipe)
(def m {:a {:b {:c "hi!"}}}) (-> m :a :b :c) ; "hi!" ((comp :a :b :c) m) ; nil ((comp :c :b :a) m) ; "hi!"
- regular expressions
re-find
seems pretty useful(re-find #"^left-" "left-eye") ; => "left-" (re-find #"^left-" "cleft-chin") ; => nil (re-find #"^left-" "wongleblart") ; => nil
- time
(time (vampire-related-details 0)) (Thread/sleep 1000)
- cons
cons(truct) a seq conj(oin) an item
=> (def m (seq {:a 1 :b 2 :c 3})) ([:a 1] [:b 2] [:c 3]) => (into {} m) {:a 1, :b 2, :c 3}
- require
(require ’[the-divine-cheese-code.visualization.svg :as svg])
… cheaply (use ’[the-divine-cheese-code.visualization.svg :as svg])
2 previous
## learning clojure
I will only concentrate on clojurescript for now because everything JVM might be beyond me.
lumo is SELF HOSTED - which means it can’t contain any JVM components. realistically it means you cant use `core.async` - there is an alrernative lib called `andare` which can be used. for the most part when using `lumo` you will install dependencies through npm
### errors
when using andare with lumo, with the following lumo file
``` (require ’[lumo.build.api :as b])
(b/watch “src” {:main ’read.core :output-to “watch/main.js” :output-dir “watch” :optimizations :advanced :verbose true :foreign-libs [{:file “src” :module-type :es6}] :watch-fn (fn [] (println “Updated build”)) :target :nodejs}) ```
it would work once, but on second compile without deleting `watch/` the following error would happen ``` #error {:message failed compiling src/read/core.cljs, :data {:file src/read/core.cljs}, :cause #error {:message Could not require cljs.core.async, :data {:tag :cljs/analysis-error}, :cause #object[Error Error: No eval-fn set]}} ```
I haven’t actually been able to fix this yet. The watch just doesn’t work - I have to delete the watch folder every single time i want to regenerate the resources.
this tutorial [seems very good about showing how core.async actually works](https://www.bradcypert.com/clojure-async/)