| |
| | (ns e2-75-76
(:use clojure.test))
An interesting discussion of the expression problem can be found
here.
| |
| | (defn make-from-mag-ang [r a]
(fn [op]
(cond (= op 'real-part) (* r (-> a Math/toRadians Math/cos))
(= op 'imag-part) (* r (-> a Math/toRadians Math/sin))
(= op 'magnitude) r
(= op 'angle) a
:else (assert false))))
| | (deftest test-make-from-mag-ang
(is (= ((make-from-mag-ang 10 90) 'imag-part) 10))
(is (= ((make-from-mag-ang 10 90) 'magnitude) 10))
(is (= ((make-from-mag-ang 10 90) 'angle) 90)))
Which strategy suits best when new functions are added more often?
The data-centric programming suits better in this case as we don't need to
modify datatypes when new operations are added. If we were to choose message
passing we would need to modify each datatype present in the system when new
function (operation) got added.
| |
| |