Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code to use Clojure idioms for clarity #159

Closed
wants to merge 12 commits into from
32 changes: 16 additions & 16 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{:tasks {test (shell "clojure -X:test")
lint (do (run 'style:cljfmt)
(run 'style:cljstyle)
(run 'lint:eastwood)
(run 'lint:kondo))
style:fix (do (run 'style:fix:cljfmt)
(run 'style:fix:cljstyle))
style:cljfmt (shell "cljfmt check")
style:cljstyle (shell "cljstyle check")
style:fix:cljfmt (shell "cljfmt fix")
style:fix:cljstyle (shell "cljstyle fix")
lint:eastwood (shell "clojure -M:test:eastwood")
lint:kondo (shell "clj-kondo --lint test src")
pre-push (do (run 'style:fix)
(run 'test)
(run 'lint))}}
{:tasks {test (shell "clojure -X:test")
lint (do (run 'style:cljfmt)
(run 'style:cljstyle)
(run 'lint:eastwood)
(run 'lint:kondo))
fix (do (run 'fix:cljfmt)
(run 'fix:cljstyle))
style:cljfmt (shell "cljfmt check")
style:cljstyle (shell "cljstyle check")
fix:cljfmt (shell "cljfmt fix")
fix:cljstyle (shell "cljstyle fix")
lint:eastwood (shell "clojure -M:test:eastwood")
lint:kondo (shell "clj-kondo --lint test src")
pre-push (do (run 'style:fix)
(run 'test)
(run 'lint))}}
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_1/book_2_1.clj
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
[x y]
(let [dispatch (fn [m]
(cond
(= m 0) x
(zero? m) x
(= m 1) y
:else (throw (Exception. "Argument not 0 or 1: CONS"))))]
dispatch))
Expand Down
4 changes: 2 additions & 2 deletions src/sicp/chapter_2/part_1/ex_2_01.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
(defn make-rat-2
[numer denom]
(let [devider (abs (m/gcd numer denom))
signed-numer (cond (and (< denom 0) (< numer 0)) (abs numer)
(and (< denom 0) (> numer 0)) (- numer)
signed-numer (cond (and (neg? denom) (neg? numer)) (abs numer)
(and (neg? denom) (pos? numer)) (- numer)
:else numer)
signed-denom (abs denom)]
(m/pair (/ signed-numer devider)
Expand Down
10 changes: 4 additions & 6 deletions src/sicp/chapter_2/part_1/ex_2_03.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@

(defn length-segment
[segment]
(Math/sqrt (+ (Math/pow (- (ex-2-02/x-point (ex-2-02/start-segment segment))
(ex-2-02/x-point (ex-2-02/end-segment segment)))
2)
(Math/pow (- (ex-2-02/y-point (ex-2-02/start-segment segment))
(ex-2-02/y-point (ex-2-02/end-segment segment)))
2))))
(Math/hypot (- (ex-2-02/x-point (ex-2-02/start-segment segment))
(ex-2-02/x-point (ex-2-02/end-segment segment)))
(- (ex-2-02/y-point (ex-2-02/start-segment segment))
(ex-2-02/y-point (ex-2-02/end-segment segment)))))

(defn make-rectangle
[segment-width segment-height]
Expand Down
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_1/ex_2_06.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
; (not in terms of repeated application of add-1).

(def zero (fn [_] (fn [x] x)))
(def one (fn [f] (fn [x] (f x))))
(def one (fn [f] f))
(def two (fn [f] (fn [x] (f (f x)))))

(defn add-1
Expand Down
4 changes: 1 addition & 3 deletions src/sicp/chapter_2/part_1/ex_2_10.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

(defn div-interval
[interval-1 interval-2]
(if (< (* (m/lower-bound interval-2)
(m/upper-bound interval-2))
0)
(if (neg? (* (m/lower-bound interval-2) (m/upper-bound interval-2)))
(m/error "interval-2 is spanning zero")
(m/mul-interval
interval-1
Expand Down
24 changes: 12 additions & 12 deletions src/sicp/chapter_2/part_1/ex_2_11.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

(defn interval-case
[low high]
(cond (and (< low 0) (< high 0)) -1
(and (< low 0) (> high 0)) 0
(cond (and (neg? low) (neg? high)) -1
(and (neg? low) (pos? high)) 0
:else 1))

(defn mul-interval
Expand All @@ -68,21 +68,21 @@
; - - - -
(= i2-case -1) (m/make-interval (* i1-hi i2-hi) (* i1-lo i2-lo))
; - - - +
(= i2-case 0) (m/make-interval (* i1-lo i2-hi) (* i1-lo i2-lo))
(zero? i2-case) (m/make-interval (* i1-lo i2-hi) (* i1-lo i2-lo))
; - - + +
:else (m/make-interval (* i1-lo i2-hi) (* i1-hi i2-lo)))
(= i1-case 0) (cond
; - + - -
(= i2-case -1) (m/make-interval (* i1-hi i2-lo) (* i1-lo i2-lo))
; - + - +
(= i2-case 0) (m/make-interval (min (* i1-lo i2-hi) (* i1-hi i2-lo))
(max (* i1-lo i2-lo) (* i1-hi i2-hi)))
; - + + +
:else (m/make-interval (* i1-lo i2-hi) (* i1-hi i2-hi)))
(zero? i1-case) (cond
; - + - -
(= i2-case -1) (m/make-interval (* i1-hi i2-lo) (* i1-lo i2-lo))
; - + - +
(zero? i2-case) (m/make-interval (min (* i1-lo i2-hi) (* i1-hi i2-lo))
(max (* i1-lo i2-lo) (* i1-hi i2-hi)))
; - + + +
:else (m/make-interval (* i1-lo i2-hi) (* i1-hi i2-hi)))
:else (cond
; + + - -
(= i2-case -1) (m/make-interval (* i1-hi i2-lo) (* i1-lo i2-hi))
; + + - +
(= i2-case 0) (m/make-interval (* i1-hi i2-lo) (* i1-hi i2-hi))
(zero? i2-case) (m/make-interval (* i1-hi i2-lo) (* i1-hi i2-hi))
; + + + +
:else (m/make-interval (* i1-lo i2-lo) (* i1-hi i2-hi))))))
38 changes: 12 additions & 26 deletions src/sicp/chapter_2/part_2/book_2_2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
(defn list-ref
[lst index]
(cond
(< index 0) nil
(= index 0) (first lst)
(neg? index) nil
(zero? index) (first lst)
:else (list-ref (rest lst) (dec index))))

(comment
Expand All @@ -45,15 +45,15 @@
[items]
(if (m/list-empty? items)
0
(+ 1 (length-recursice (m/cdr items)))))
(inc (length-recursice (m/cdr items)))))

(comment
(length-recursice (list 1 3 5 7))) ; 4

(defn length
[list]
(if (seq list)
(+ 1 (length (rest list)))
(inc (length (rest list)))
0))

(comment
Expand All @@ -71,21 +71,14 @@

(defn scale-list
[items factor]
(if (m/list-empty? items)
nil
(cons (* (m/car items) factor)
(scale-list (m/cdr items)
factor))))
(when-not (m/list-empty? items) (cons (* (m/car items) factor) (scale-list (m/cdr items) factor))))

(comment
(scale-list (list 1 2 3 4 5) 10)) ; (10 20 30 40 50)

(defn my-map
[proc items]
(if (m/list-empty? items)
nil
(cons (proc (m/car items))
(my-map proc (m/cdr items)))))
(when-not (m/list-empty? items) (cons (proc (m/car items)) (my-map proc (m/cdr items)))))

(comment
(my-map abs (list -10 2.5 -11.6 17)) ; (10 2.5 11.6 17)
Expand Down Expand Up @@ -164,12 +157,7 @@
[n]
(letfn [(next
[k]
(if (> k n)
nil
(let [f (fib k)]
(if (even? f)
(cons f (next (inc k)))
(next (inc k))))))]
(when-not (> k n) (let [f (fib k)] (if (even? f) (cons f (next (inc k))) (next (inc k))))))]
(next 0)))

(defn my-filter
Expand All @@ -189,9 +177,7 @@

(defn enumerate-interval
[low high]
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
(when-not (> low high) (cons low (enumerate-interval (inc low) high))))

(defn enumerate-tree
[tree]
Expand Down Expand Up @@ -245,7 +231,7 @@
(accumulate append nil
(map (fn [i]
(map (fn [j] (list i j))
(enumerate-interval 1 (- i 1))))
(enumerate-interval 1 (dec i))))
(enumerate-interval 1 6)))) ; (1 2 3 4 5 6)
; ((2 1) (3 1) (3 2) (4 1) (4 2) (4 3) (5 1) (5 2) (5 3) (5 4) (6 1) (6 2) (6 3) (6 4) (6 5))

Expand All @@ -269,12 +255,12 @@
(filter prime-sum? (flatmap
(fn [i]
(map (fn [j] (list i j))
(enumerate-interval 1 (- i 1))))
(enumerate-interval 1 (dec i))))
(enumerate-interval 1 n)))))

(defn my-remove
[item sequence]
(filter (fn [x] (not (= x item))) sequence))
(filter (fn [x] (not= x item)) sequence))

(defn permutations
[s]
Expand Down Expand Up @@ -388,7 +374,7 @@

(defn corner-split
[painter n]
(if (= n 0)
(if (zero? n)
painter
(let [up (corner-split painter (dec n))
right (corner-split painter (dec n))
Expand Down
4 changes: 2 additions & 2 deletions src/sicp/chapter_2/part_2/ex_2_19.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@

(defn cc
[amount coin-values]
(cond (= amount 0) 1
(or (< amount 0)
(cond (zero? amount) 1
(or (neg? amount)
(no-more? coin-values)) 0
:else (+ (cc amount (except-first-denomination coin-values))
(cc (- amount (first-denomination coin-values)) coin-values))))
3 changes: 1 addition & 2 deletions src/sicp/chapter_2/part_2/ex_2_21.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

(defn square-list-1
[items]
(if (m/list-empty? items)
nil
(when-not (m/list-empty? items)
(cons (* (m/car items) (m/car items))
(square-list-1 (m/cdr items)))))

Expand Down
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_2/ex_2_23.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
(for-each proc (m/cdr items)))))

(comment
(for-each #(println %) (list 57 321 88)))
(for-each println (list 57 321 88)))
3 changes: 1 addition & 2 deletions src/sicp/chapter_2/part_2/ex_2_36.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

(defn accumulate-n
[op init seqs]
(if (nil? (m/car seqs))
nil
(when-not (nil? (m/car seqs))
(cons (b22/accumulate op init (map m/car seqs))
(accumulate-n op init (map m/cdr seqs)))))
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_2/ex_2_40.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(b22/flatmap
(fn [i]
(map (fn [j] (list i j))
(b22/enumerate-interval 1 (- i 1))))
(b22/enumerate-interval 1 (dec i))))
(b22/enumerate-interval 1 n)))

(defn prime-sum-pairs
Expand Down
4 changes: 2 additions & 2 deletions src/sicp/chapter_2/part_2/ex_2_42.clj
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@
[board-size]
(letfn [(queen-cols
[k]
(if (= 0 k)
(if (zero? k)
(list nil) ; It's `empty-board`
(filter (fn [positions] (safe? k positions))
(mapcat (fn [rest-of-queens]
(map (fn [new-row]
(adjoin-position new-row k rest-of-queens))
(b22/enumerate-interval 1 board-size)))
(queen-cols (- k 1))))))]
(queen-cols (dec k))))))]
(queen-cols board-size)))
8 changes: 4 additions & 4 deletions src/sicp/chapter_2/part_2/ex_2_43.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@
[board-size]
(letfn [(queen-cols
[k]
(if (= 0 k)
(if (zero? k)
(list nil)
(filter (fn [positions] (ex-2-42/safe? k positions))
(mapcat (fn [rest-of-queens]
(map (fn [new-row]
(ex-2-42/adjoin-position new-row k rest-of-queens))
(b22/enumerate-interval 1 board-size)))
(queen-cols (- k 1))))))]
(queen-cols (dec k))))))]
(queen-cols board-size)))

(defn queens-2-43
[board-size]
(letfn [(queen-cols
[k]
(if (= 0 k)
(if (zero? k)
(list nil)
(filter (fn [positions] (ex-2-42/safe? k positions))
(mapcat (fn [new-row]
; diff 1
(map (fn [rest-of-queens]
; diff 1
(ex-2-42/adjoin-position new-row k rest-of-queens))
(queen-cols (- k 1)))) ; diff 2
(queen-cols (dec k)))) ; diff 2
(b22/enumerate-interval 1 board-size)))))] ; diff 2
(queen-cols board-size)))
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_2/ex_2_44.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(defn up-split
[painter n]
; Just a potential solution...
(if (= n 0)
(if (zero? n)
painter
(let [down (up-split painter (dec n))
up (up-split painter (dec n))]
Expand Down
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_2/ex_2_45.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(defn split
[primary secondary]
(fn [painter n]
(if (= n 0)
(if (zero? n)
painter
(let [smaller ((split primary secondary) painter (dec n))
primary-split (primary painter smaller)
Expand Down
6 changes: 3 additions & 3 deletions src/sicp/chapter_2/part_3/book_2_3.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
(defn make-sum
[a1 a2]
(cond
(= a1 0) a2
(= a2 0) a1
(zero? a1) a2
(zero? a2) a1
(and (number? a1) (number? a2)) (+ a1 a2)
:else (list '+ a1 a2)))

Expand Down Expand Up @@ -242,7 +242,7 @@
(defn choose-branch
[bit branch]
(cond
(= bit 0) (left-branch-h branch)
(zero? bit) (left-branch-h branch)
(= bit 1) (right-branch-h branch)
:else (throw (Exception. (str "bad bit: CHOOSE-BRANCH " bit)))))

Expand Down
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_3/ex_2_56.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(defn make-exponentiation
[base exponent]
(cond
(= exponent 0) 1.0
(zero? exponent) 1.0
(and (= exponent 1) (number? base)) (float base)
(and (number? base) (number? exponent)) (Math/pow base exponent)
:else (list '** base exponent)))
Expand Down
Loading
Loading