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

Implement cljstyle check #157

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .cljstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{:files {:extensions #{"cljc" "cljs" "clj" "cljx"},
:ignore #{".hg" ".git"}},
:rules {:namespaces {:enabled? true,
:indent-size 2,
:break-libs? true,
:import-break-width 100},
:whitespace {:enabled? true,
:remove-surrounding? true,
:remove-trailing? true,
:insert-missing? true},
:comments {:enabled? true,
:inline-prefix " ",
:leading-prefix " "},
:functions {:enabled? true},
:eof-newline {:enabled? true,
:trailing-blanks? false},
:types {:enabled? true,
:types? true,
:protocols? true,
:reifies? true,
:proxies? true},
:blank-lines {:enabled? true,
:trim-consecutive? true,
:max-consecutive 1,
:insert-padding? true,
:padding-lines 1},
:indentation {:enabled? true,
:list-indent 2,
:indents {are [[:block 2]],
when-first [[:block 1]],
cond->> [[:block 1]],
while [[:block 1]],
#"^def" [[:inner 0]],
try [[:block 0]],
bound-fn [[:inner 0]],
thrown-with-msg? [[:block 2]],
match [[:block 1]],
testing [[:block 1]],
if-not [[:block 1]],
doseq [[:block 1]],
finally [[:block 0]],
deftype [[:block 1] [:inner 1]],
when-let [[:block 1]],
go [[:block 0]],
if-some [[:block 1]],
with-precision [[:block 1]],
let [[:block 1]],
defstruct [[:block 1]],
doto [[:block 1]],
future [[:block 0]],
fn [[:inner 0]],
alt! [[:block 0]],
as-> [[:block 1]],
do [[:block 0]],
when-not [[:block 1]],
when [[:block 1]],
extend [[:block 1]],
go-loop [[:block 1]],
defn [[:inner 0]],
if [[:block 1]],
ns [[:block 1]],
deftest [[:inner 0]],
extend-type [[:block 1] [:inner 1]],
defmethod [[:inner 0]],
struct-map [[:block 1]],
extend-protocol [[:block 1] [:inner 1]],
cond-> [[:block 1]],
dotimes [[:block 1]],
reify [[:inner 0] [:inner 1]],
with-open [[:block 1]],
defonce [[:inner 0]],
defn- [[:inner 0]],
alt!! [[:block 0]],
defprotocol [[:block 1] [:inner 1]],
letfn [[:block 1] [:inner 2 0]],
use-fixtures [[:inner 0]],
loop [[:block 1]],
with-out-str [[:block 0]],
condp [[:block 2]],
cond [[:block 0]],
for [[:block 1]],
binding [[:block 1]],
with-local-vars [[:block 1]],
defmacro [[:inner 0]],
proxy [[:block 2] [:inner 1]],
with-redefs [[:block 1]],
locking [[:block 1]],
defmulti [[:inner 0]],
if-let [[:block 1]],
case [[:block 1]],
catch [[:block 2]],
thread [[:block 0]],
comment [[:block 0]],
#"^with-" [[:inner 0]],
defrecord [[:block 1] [:inner 1]],
thrown? [[:block 1]],
when-some [[:block 1]],
def [[:inner 0]]}},
:vars {:enabled? true}}}
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ jobs:
cli: 'latest'
bb: 'latest'
cljfmt: 'latest'
cljstyle: 'latest'

- name: 👍 cljfmt
run: bb lint:check
run: bb style:cljfmt

- name: 👍 cljstyle
run: bb style:cljstyle


experimental:
Expand Down
27 changes: 16 additions & 11 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{:tasks {test (shell "clojure -X:test")
lint (do (run 'lint:check)
(run 'lint:eastwood)
(run 'lint:kondo))
lint:fix (shell "cljfmt fix")
lint:check (shell "cljfmt check")
lint:eastwood (shell "clojure -M:test:eastwood")
lint:kondo (shell "clj-kondo --lint test src")
pre-push (do (run 'lint: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))
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))}}
68 changes: 38 additions & 30 deletions src/sicp/chapter_1/part_1/book_1_1.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns sicp.chapter-1.part-1.book-1-1)

(comment "1.1.1")
; Expressions --------------------------------------------------------------------------------------
(comment "1.1.1 Expressions ----------------------------------------------------------------------")

(+ 137 349) ; 486
(- 1000 334) ; 666
Expand All @@ -19,8 +18,7 @@
(+ (- 10 7)
6)) ; 57

(comment "1.1.2")
; Naming and the Environment -----------------------------------------------------------------------
(comment "1.1.2 Naming and the Environment -------------------------------------------------------")

(def size 2)
(* 5 size) ; 10
Expand All @@ -30,33 +28,33 @@
(* pi (* radius radius)) ; 314.159
(def circumference (* 2 pi radius)) ; 62.8318

(comment "1.1.3")
; Evaluating Combinations --------------------------------------------------------------------------
(comment "1.1.3 Evaluating Combinations ----------------------------------------------------------")

(* (+ 2 (* 4 6)) (+ 3 5 7)) ; 390

(comment "1.1.4")
; Compound Procedures ------------------------------------------------------------------------------
(comment "1.1.4 Compound Procedures --------------------------------------------------------------")

(defn square [x]
(defn square
[x]
(* x x))

(square 21) ; 441
(square (+ 2 5)) ; 49
(square (square 3)) ; 81

(defn sum-of-squares [x y]
(defn sum-of-squares
[x y]
(+ (square x) (square y)))

(sum-of-squares 3 4) ; 25

(defn f [a]
(defn f
[a]
(sum-of-squares (+ a 1) (* a 2)))

(f 5) ; 136

(comment "1.1.5")
; The Substitution Model for Procedure Application -------------------------------------------------
(comment "1.1.5 The Substitution Model for Procedure Application ---------------------------------")

(f 5) ; 136
(sum-of-squares (+ 5 1) (* 5 2)) ; 136
Expand All @@ -70,8 +68,8 @@
(+ (* (+ 5 1) (+ 5 1))
(* (* 5 2) (* 5 2))) ; 136

(comment "1.1.6")
; Conditional Expressions and Predicates -----------------------------------------------------------
(comment "1.1.6 Conditional Expressions and Predicates -------------------------------------------")

; Exercises:
; * 1.1
; * 1.2
Expand All @@ -86,36 +84,43 @@
(= x 0) 0
(< x 0) (- x)))

(defn abs-2 [x]
(defn abs-2
[x]
(cond (< x 0) (- x)
:else x))

(defn abs-3 [x]
(defn abs-3
[x]
(if (< x 0)
(- x)
x))

; Renamed due to Clojure conflit names
(defn >=-1 [x y]
(defn >=-1
[x y]
(or (> x y) (= x y)))

(defn >=-2 [x y]
(defn >=-2
[x y]
(not (< x y)))

(comment "1.1.7")
; Example: Square Roots by Newton’s Method ---------------------------------------------------------
(comment "1.1.7 Example: Square Roots by Newton’s Method -----------------------------------------")

; Exercises:
; * 1.6
; * 1.7
; * 1.8

(defn average [x y]
(defn average
[x y]
(/ (+ x y) 2))

(defn improve [guess x]
(defn improve
[guess x]
(average guess (/ x guess)))

(defn good-enough? [guess x]
(defn good-enough?
[guess x]
(< (abs (- (square guess) x)) 0.001))

(defn sqrt-iter
Expand All @@ -124,19 +129,22 @@
guess
(sqrt-iter (improve guess x) x)))

(defn sqrt [x]
(defn sqrt
[x]
(sqrt-iter 1.0 x))

(comment "1.1.8")
; Procedures as Black-Box Abstractions -------------------------------------------------------------
(comment "1.1.8 Procedures as Black-Box Abstractions ---------------------------------------------")

(defn square-alt [x]
(defn square-alt
[x]
(Math/exp (double (Math/log x))))

(defn sqrt-alt [x]
(defn sqrt-alt
[x]
(letfn [(good-enough? [guess x] (< (abs (- (* guess guess) x)) 0.001))
(improve [guess x] (/ (+ guess (/ x guess)) 2))
(sqrt-iter [guess x]
(sqrt-iter
[guess x]
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))]
Expand Down
5 changes: 4 additions & 1 deletion src/sicp/chapter_1/part_1/ex_1_05.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
; Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is
; using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:

(defn p [] p)
(defn p
[]
p)

(defn test_1_5
[x y]
(if (= x 0) 0 y))
Expand Down
3 changes: 2 additions & 1 deletion src/sicp/chapter_1/part_1/ex_1_06.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns sicp.chapter-1.part-1.ex-1-06
(:require [sicp.chapter-1.part-1.book-1-1 :as b11]))
(:require
[sicp.chapter-1.part-1.book-1-1 :as b11]))

; Exercise 1.6
; Alyssa P. Hacker doesn’t see why if needs to be provided as a special form.
Expand Down
9 changes: 6 additions & 3 deletions src/sicp/chapter_1/part_1/ex_1_07.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns sicp.chapter-1.part-1.ex-1-07
(:require [sicp.chapter-1.part-1.book-1-1 :as b11]))
(:require
[sicp.chapter-1.part-1.book-1-1 :as b11]))

; Exercise 1.7
; The `good-enough?` test used in computing square roots will not be very effective
Expand All @@ -16,10 +17,12 @@
; Does this work better for small and large numbers?

; Imporovments
(defn good-enough-v2? [guess1 guess2]
(defn good-enough-v2?
[guess1 guess2]
(< (abs (- guess1 guess2)) 0.001))

(defn sqrt-iter-v2 [guess x]
(defn sqrt-iter-v2
[guess x]
(if (good-enough-v2? guess (b11/improve guess x))
guess
(sqrt-iter-v2 (b11/improve guess x) x)))
3 changes: 2 additions & 1 deletion src/sicp/chapter_1/part_1/ex_1_08.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns sicp.chapter-1.part-1.ex-1-08
(:require [sicp.chapter-1.part-1.book-1-1 :as b11]))
(:require
[sicp.chapter-1.part-1.book-1-1 :as b11]))

; Exercise 1.8
; Newton’s method for cube roots is based on the fact that if y
Expand Down
Loading
Loading