Skip to content

Instantly share code, notes, and snippets.

@reedho
Created July 26, 2016 03:06
Show Gist options
  • Save reedho/1d6ee5462c5d59fa6036c1c61d4c26e5 to your computer and use it in GitHub Desktop.
Save reedho/1d6ee5462c5d59fa6036c1c61d4c26e5 to your computer and use it in GitHub Desktop.
Clojure 101
;; Add dependencies live on lein repl
;; ------------------------------
; in project.clj, add to dependencies vector: [com.cemerick/pomegranate "0.3.0"]
[com.cemerick/pomegranate "0.3.1"]
; example adding deps via repl
(cemerick.pomegranate/add-dependencies
:coordinates '[[midje "1.8.2"]]
:repositories (merge cemerick.pomegranate.aether/maven-central
{"clojars" "http://clojars.org/repo"}))
;; Stuart Sierra's System:
;; ------------------------------
; reloaded.repl is best practice workflow working with System.
; It provides small function which working around 2 var: system and initializer
(set-init! init-fn) ;; alter initializer to init-fn in first arg
(init) ;; call component/stop if system not nil then call (init) saved to system
(start) ;; save system with result of component/start
(stop) ;; save system with result of component/stop
(go) ;; call (init) followed by (start)
(clear) ;; stop the system and save nil to system
(reset) ;; call (clear) followed by (clojure.tools.namespace.repl/refresh :after go)
;; Using clj-wallhack to access privates
;; ------------------------------
(wall.hack/field org.httpkit.BytesInputStream :buf __obj) ;; => byte-array
; Note: In cider, inspect on object instance will reveals all its fields,
; whereas inspecting on class will show all public methods.
;; Runtime reflection / meta programming
;; ------------------------------
; Symbol: 'parse, `parse
; Var: #'parse, (var parse), (resolve 'parse)
; `resolve` run on runtime whereas `var` is used during compilation, e.g. #'parse is expanded as (var parse)
; Clojure call function via its string:
(-> "+" symbol resolve deref (apply (range 10)))
;; Evaluating protocol will result a map which contains useful information of the protocol itself.
;; ------------------------------
;; Find out symbols defined in an ns
;; ------------------------------
(-> 'cemerick.pomegranate find-ns ns-publics keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment