-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor parts of SICP chapter 2's code
The code refactor focused on the parts related to packages in chapter 2 of SICP. It included separating different packages into their own files, implementing a generic operation system, and standardizing the function interface. This has resulted in cleaner, more maintainable, and more understandable code.
- Loading branch information
Denis Smet
committed
Feb 18, 2024
1 parent
09a8cf4
commit 6ed9882
Showing
12 changed files
with
257 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(ns sicp.chapter-2.packages.install-complex | ||
(:require | ||
[sicp.chapter-2.packages.op-table :as ot] | ||
[sicp.chapter-2.part-4.book-2-4 :as b24])) | ||
|
||
(defn install-complex-package | ||
[] | ||
; Imported procedures from rectangular and polar packages | ||
(letfn [(make-from-real-imag [x y] ((ot/get :make-from-real-imag :rectangular) x y)) | ||
(make-from-mag-ang [r a] ((ot/get :make-from-mag-ang :polar) r a)) | ||
; Internal procedures | ||
(add-complex | ||
[z1 z2] | ||
(make-from-real-imag (+ (b24/real-part z1) (b24/real-part z2)) | ||
(+ (b24/imag-part z1) (b24/imag-part z2)))) | ||
(sub-complex | ||
[z1 z2] | ||
(make-from-real-imag (- (b24/real-part z1) (b24/real-part z2)) | ||
(- (b24/imag-part z1) (b24/imag-part z2)))) | ||
(mul-complex | ||
[z1 z2] | ||
(make-from-mag-ang (* (b24/magnitude z1) (b24/magnitude z2)) | ||
(+ (b24/angle z1) (b24/angle z2)))) | ||
(div-complex | ||
[z1 z2] | ||
(make-from-mag-ang (/ (b24/magnitude z1) (b24/magnitude z2)) | ||
(- (b24/angle z1) (b24/angle z2)))) | ||
; Interface to rest of the system | ||
(tag [z] (ot/attach-tag :complex z))] | ||
; Putting functions in a map or registry | ||
(ot/put :add [:complex :complex] (fn [z1 z2] (tag (add-complex z1 z2)))) | ||
(ot/put :sub [:complex :complex] (fn [z1 z2] (tag (sub-complex z1 z2)))) | ||
(ot/put :mul [:complex :complex] (fn [z1 z2] (tag (mul-complex z1 z2)))) | ||
(ot/put :div [:complex :complex] (fn [z1 z2] (tag (div-complex z1 z2)))) | ||
(ot/put :make-from-real-imag :complex (fn [x y] (tag (make-from-real-imag x y)))) | ||
(ot/put :make-from-mag-ang :complex (fn [r a] (tag (make-from-mag-ang r a)))) | ||
:done)) | ||
|
||
(defn make-complex-from-real-imag | ||
[x y] | ||
((ot/get :make-from-real-imag :complex) x y)) | ||
|
||
(defn make-complex-from-mag-ang | ||
[r a] | ||
((ot/get :make-from-mag-ang :complex) r a)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(ns sicp.chapter-2.packages.install-polar | ||
(:require | ||
[sicp.chapter-2.packages.op-table :as ot])) | ||
|
||
(defn install-polar-package | ||
[] | ||
(letfn [(magnitude [z] (first z)) | ||
(angle [z] (second z)) | ||
(make-from-mag-ang [r a] (list r a)) | ||
(real-part [z] (* (magnitude z) (Math/cos (angle z)))) | ||
(imag-part [z] (* (magnitude z) (Math/sin (angle z)))) | ||
(make-from-real-imag | ||
[x y] | ||
(list (Math/sqrt (+ (Math/pow x 2) (Math/pow y 2))) | ||
(Math/atan2 y x))) | ||
(tag [x] (cons :polar x))] | ||
; interface to the rest of the system | ||
(ot/put :real-part :polar real-part) | ||
(ot/put :imag-part :polar imag-part) | ||
(ot/put :magnitude :polar magnitude) | ||
(ot/put :angle :polar angle) | ||
(ot/put :make-from-real-imag :polar (fn [x y] (tag (make-from-real-imag x y)))) | ||
(ot/put :make-from-mag-ang :polar (fn [r a] (tag (make-from-mag-ang r a)))) | ||
:done)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
(ns sicp.chapter-2.packages.install-rational | ||
(:require | ||
[sicp.chapter-2.packages.op-table :as ot] | ||
[sicp.misc :as m])) | ||
|
||
(defn install-rational-package | ||
[] | ||
; Internal procedures | ||
(letfn [(numer [x] (first x)) | ||
(denom [x] (second x)) | ||
(make-rat | ||
[n d] | ||
(let [g (m/gcd n d)] | ||
(m/pair (/ n g) (/ d g)))) | ||
(add-rat | ||
[x y] | ||
(make-rat (+ (* (numer x) (denom y)) | ||
(* (numer y) (denom x))) | ||
(* (denom x) (denom y)))) | ||
(sub-rat | ||
[x y] | ||
(make-rat (- (* (numer x) (denom y)) | ||
(* (numer y) (denom x))) | ||
(* (denom x) (denom y)))) | ||
(mul-rat | ||
[x y] | ||
(make-rat (* (numer x) (numer y)) | ||
(* (denom x) (denom y)))) | ||
(div-rat | ||
[x y] | ||
(make-rat (* (numer x) (denom y)) | ||
(* (denom x) (numer y)))) | ||
; Interface to rest of the system | ||
(tag [x] (ot/attach-tag :rational x))] | ||
(ot/put :add [:rational :rational] (fn [x y] (tag (add-rat x y)))) | ||
(ot/put :sub [:rational :rational] (fn [x y] (tag (sub-rat x y)))) | ||
(ot/put :mul [:rational :rational] (fn [x y] (tag (mul-rat x y)))) | ||
(ot/put :div [:rational :rational] (fn [x y] (tag (div-rat x y)))) | ||
(ot/put :make :rational (fn [n d] (tag (make-rat n d)))) | ||
:done)) | ||
|
||
(defn make-rational | ||
[n d] | ||
((ot/get :make :rational) n d)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
(ns sicp.chapter-2.packages.install-rectangular | ||
(:require | ||
[sicp.chapter-2.packages.op-table :as ot])) | ||
|
||
(defn install-rectangular-package | ||
[] | ||
; internal procedures | ||
(letfn [(real-part [z] (first z)) | ||
(imag-part [z] (second z)) | ||
(make-from-real-imag [x y] (list x y)) | ||
(magnitude | ||
[z] | ||
(Math/sqrt (+ (Math/pow (real-part z) 2) | ||
(Math/pow (imag-part z) 2)))) | ||
(angle [z] (Math/atan2 (imag-part z) (real-part z))) | ||
(make-from-mag-ang [r a] (list (* r (Math/cos a)) (* r (Math/sin a)))) | ||
(tag [x] (ot/attach-tag :rectangular x))] | ||
; interface to the rest of the system | ||
(ot/put :real-part '(:rectangular) real-part) | ||
(ot/put :imag-part '(:rectangular) imag-part) | ||
(ot/put :magnitude '(:rectangular) magnitude) | ||
(ot/put :angle '(:rectangular) angle) | ||
(ot/put :make-from-real-imag :rectangular (fn [x y] (tag (make-from-real-imag x y)))) | ||
(ot/put :make-from-mag-ang :rectangular (fn [r a] (tag (make-from-mag-ang r a)))) | ||
:done)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
(ns sicp.chapter-2.packages.install-scheme-number | ||
(:require | ||
[sicp.chapter-2.packages.op-table :as ot])) | ||
|
||
(defn install-scheme-number-package | ||
[] | ||
(letfn [(tag [x] (ot/attach-tag :scheme-number x))] | ||
(ot/put :add [:scheme-number :scheme-number] (fn [x y] (tag (+ x y)))) | ||
(ot/put :sub [:scheme-number :scheme-number] (fn [x y] (tag (- x y)))) | ||
(ot/put :mul [:scheme-number :scheme-number] (fn [x y] (tag (* x y)))) | ||
(ot/put :div [:scheme-number :scheme-number] (fn [x y] (tag (/ x y)))) | ||
(ot/put :make :scheme-number (fn [x] (tag x))) | ||
:done)) | ||
|
||
(defn make-scheme-number | ||
[n] | ||
((ot/get :make :scheme-number) n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(ns sicp.chapter-2.packages.op-table | ||
(:refer-clojure :exclude [get]) | ||
(:require | ||
[clojure.pprint :as pp])) | ||
|
||
(comment "Custom helper functions. Made myself, just for testing ---------------------------------") | ||
|
||
(def operation-table (atom {})) | ||
|
||
(defn attach-tag | ||
[tag value] | ||
{:tag tag, :contents value}) | ||
|
||
(defn put | ||
[operation types procedure] | ||
(swap! operation-table assoc-in [operation types] procedure)) | ||
|
||
(defn get | ||
[operation types] | ||
(get-in @operation-table [operation types])) | ||
|
||
(defn apply-generic | ||
[op & args] | ||
(let [type-tags (map #(-> % :tag) args) | ||
operation (get op type-tags)] | ||
(if operation | ||
(apply operation (map #(:contents %) args)) | ||
(throw (IllegalArgumentException. (str "No method for: " op)))))) | ||
|
||
(defn inspect | ||
[] | ||
(->> @operation-table | ||
(map (fn [item] | ||
{:operation (first item) | ||
:types (first (first (into '() (second item))))})) | ||
(pp/print-table))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,30 @@ | ||
(ns sicp.chapter-2.part-5.book-2-5 | ||
(:require | ||
[sicp.chapter-2.part-4.book-2-4 :as b24] | ||
[sicp.misc :as m])) | ||
[sicp.chapter-2.packages.op-table :as ot])) | ||
|
||
(comment "2.5 Systems with Generic Operations ----------------------------------------------------") | ||
|
||
(comment "2.5.1 Generic Arithmetic Operations ----------------------------------------------------") | ||
|
||
; Custom helper functions. Made myself, just for testing ------------------------------------------- | ||
|
||
(def operation-table (atom {})) | ||
|
||
(defn attach-tag | ||
[tag value] | ||
{:tag tag, :contents value}) | ||
|
||
(defn put-op | ||
[operation types procedure] | ||
(swap! operation-table assoc-in [operation types] procedure)) | ||
|
||
(defn get-op | ||
[operation types] | ||
(get-in @operation-table [operation types])) | ||
|
||
(defn apply-generic | ||
[op & args] | ||
(let [type-tags (map #(-> % :tag) args) | ||
operation (get-op op type-tags)] | ||
(if operation | ||
(apply operation (map #(:contents %) args)) | ||
(throw (IllegalArgumentException. (str "No method for: " op)))))) | ||
|
||
; -------------------------------------------------------------------------------------------------- | ||
|
||
(defn add | ||
[x y] | ||
(apply-generic :add x y)) | ||
(ot/apply-generic :add x y)) | ||
|
||
(defn sub | ||
[x y] | ||
(apply-generic :sub x y)) | ||
(ot/apply-generic :sub x y)) | ||
|
||
(defn mul | ||
[x y] | ||
(apply-generic :mul x y)) | ||
(ot/apply-generic :mul x y)) | ||
|
||
(defn div | ||
[x y] | ||
(apply-generic :div x y)) | ||
|
||
(defn install-scheme-number-package | ||
[] | ||
(letfn [(tag [x] (attach-tag :scheme-number x))] | ||
(put-op :add [:scheme-number :scheme-number] (fn [x y] (tag (+ x y)))) | ||
(put-op :sub [:scheme-number :scheme-number] (fn [x y] (tag (- x y)))) | ||
(put-op :mul [:scheme-number :scheme-number] (fn [x y] (tag (* x y)))) | ||
(put-op :div [:scheme-number :scheme-number] (fn [x y] (tag (/ x y)))) | ||
(put-op :make :scheme-number (fn [x] (tag x))) | ||
:done)) | ||
|
||
(defn install-rational-package | ||
[] | ||
; Internal procedures | ||
(letfn [(numer [x] (first x)) | ||
(denom [x] (second x)) | ||
(make-rat | ||
[n d] | ||
(let [g (m/gcd n d)] | ||
(m/pair (/ n g) (/ d g)))) | ||
(add-rat | ||
[x y] | ||
(make-rat (+ (* (numer x) (denom y)) | ||
(* (numer y) (denom x))) | ||
(* (denom x) (denom y)))) | ||
(sub-rat | ||
[x y] | ||
(make-rat (- (* (numer x) (denom y)) | ||
(* (numer y) (denom x))) | ||
(* (denom x) (denom y)))) | ||
(mul-rat | ||
[x y] | ||
(make-rat (* (numer x) (numer y)) | ||
(* (denom x) (denom y)))) | ||
(div-rat | ||
[x y] | ||
(make-rat (* (numer x) (denom y)) | ||
(* (denom x) (numer y)))) | ||
; Interface to rest of the system | ||
(tag [x] (attach-tag :rational x))] | ||
(put-op :add [:rational :rational] (fn [x y] (tag (add-rat x y)))) | ||
(put-op :sub [:rational :rational] (fn [x y] (tag (sub-rat x y)))) | ||
(put-op :mul [:rational :rational] (fn [x y] (tag (mul-rat x y)))) | ||
(put-op :div [:rational :rational] (fn [x y] (tag (div-rat x y)))) | ||
(put-op :make :rational (fn [n d] (tag (make-rat n d)))) | ||
:done)) | ||
|
||
(defn install-complex-package | ||
[] | ||
; Imported procedures from rectangular and polar packages | ||
(letfn [(make-from-real-imag [x y] ((get-op :make-from-real-imag :rectangular) x y)) | ||
(make-from-mag-ang [r a] ((get-op :make-from-mag-ang :polar) r a)) | ||
; Internal procedures | ||
(add-complex | ||
[z1 z2] | ||
(make-from-real-imag (+ (b24/real-part z1) (b24/real-part z2)) | ||
(+ (b24/imag-part z1) (b24/imag-part z2)))) | ||
(sub-complex | ||
[z1 z2] | ||
(make-from-real-imag (- (b24/real-part z1) (b24/real-part z2)) | ||
(- (b24/imag-part z1) (b24/imag-part z2)))) | ||
(mul-complex | ||
[z1 z2] | ||
(make-from-mag-ang (* (b24/magnitude z1) (b24/magnitude z2)) | ||
(+ (b24/angle z1) (b24/angle z2)))) | ||
(div-complex | ||
[z1 z2] | ||
(make-from-mag-ang (/ (b24/magnitude z1) (b24/magnitude z2)) | ||
(- (b24/angle z1) (b24/angle z2)))) | ||
; Interface to rest of the system | ||
(tag [z] (attach-tag :complex z))] | ||
; Putting functions in a map or registry | ||
(put-op :add [:complex :complex] (fn [z1 z2] (tag (add-complex z1 z2)))) | ||
(put-op :sub [:complex :complex] (fn [z1 z2] (tag (sub-complex z1 z2)))) | ||
(put-op :mul [:complex :complex] (fn [z1 z2] (tag (mul-complex z1 z2)))) | ||
(put-op :div [:complex :complex] (fn [z1 z2] (tag (div-complex z1 z2)))) | ||
(put-op :make-from-real-imag :complex (fn [x y] (tag (make-from-real-imag x y)))) | ||
(put-op :make-from-mag-ang :complex (fn [r a] (tag (make-from-mag-ang r a)))) | ||
:done)) | ||
|
||
(defn make-scheme-number | ||
[n] | ||
((get-op :make :scheme-number) n)) | ||
|
||
(defn make-rational | ||
[n d] | ||
((get-op :make :rational) n d)) | ||
|
||
(defn make-complex-from-real-imag | ||
[x y] | ||
((get-op :make-from-real-imag :complex) x y)) | ||
|
||
(defn make-complex-from-mag-ang | ||
[r a] | ||
((get-op :make-from-mag-ang :complex) r a)) | ||
(ot/apply-generic :div x y)) | ||
|
||
(comment | ||
"See also:" | ||
"sicp.chapter-2.packages.install-scheme-number" | ||
"sicp.chapter-2.packages.install-rational" | ||
"sicp.chapter-2.packages.install-rectangular" | ||
"sicp.chapter-2.packages.install-complex") |
Oops, something went wrong.