-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path007_clj_refactor.clj
More file actions
72 lines (50 loc) · 2.46 KB
/
007_clj_refactor.clj
File metadata and controls
72 lines (50 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(ns clojure-tip-of-the-day.007-clj-refactor
(:require [clj-http.client :as client]))
;;;; Clojure Tip of the Day episode 7: clj-refactor
;;;; See the list of available refactorings: https://github.com/clojure-emacs/clj-refactor.el/wiki
;;;; Check PurelyFunctional's "clj-refactor Reference Sheet": https://purelyfunctional.tv/clojure-resource-center/
;;; dependency, requires/imports - e.g. clj-http (Support https://clojuriststogether.org/!)
;;; `, r a p` (cljr-add-project-dependency)
;;; or `, r h d` (cljr-hotload-dependency) + manual
;;; `, r a r` (cljr-add-require-to-ns) and/or `, r a i` (cljr-add-import-to-ns)
;; download http://example.com/
;; https://github.com/dakrone/clj-http
;; With basic auth: https://github.com/dakrone/clj-http#basic-auth
(defn example-get []
(-> (client/get
"https://example.com")
:body))
(subs (example-get) 960 983)
;;; "extract function"
;;; Similarly, you can extract "constant" and "def"
;; `, r e f` (cljr-extract-function): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-extract-function
;; just-odds:
(defn- just-odds
[]
(->> (range 10)
(map inc)
(filter odd?)))
;; `, r f e` (cljr-create-fn-from-example): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-example
;; this just create a stub without body
(defn- compute-secrets [a]
(reduce + a))
(let [a (range 10)]
(compute-secrets a))
;;; `let` refactorings
;; `, r i l` (cljr-introduce-let): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-introduce-let
;; `, r e l` (cljr-expand-let): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-expand-let
;; `, r m l` (cljr-move-to-let): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-move-to-let
(let [odds (filter odd? (range 10))
even-again (map inc odds)]
(apply + even-again))
(let [odds (filter odd? (range 10))
even-again (map inc odds)]
(apply + even-again))
;;; convert to collection type: https://github.com/clojure-emacs/clj-refactor.el/wiki/clojure-convert-to-collection-type
;; `, r c [` (clojure-convert-collection-to-vector)
(reduce + [1 2 3 4 5 6 ])
;;; threading
;; `, r t l` (cljr-thread-last-all): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-thread-last-all
;; `, r u w` (cljr-unwind): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-unwind-thread
;; `, r u a` (cljr-unwind-all): https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-unwind-all
(apply + (filter odd? (map inc (range 10))))