diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2022-01-08 15:52:06 -0800 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2022-01-08 16:52:32 -0800 |
| commit | b145e66a372043b350cee870df46b81034473b70 (patch) | |
| tree | 4c4c48d3b43bd78b9cd0ed8c201c009e0fe0be69 | |
| parent | fb357b920a95257266a0063ad44d0480b66fbff2 (diff) | |
| download | perlweeklychallenge-club-b145e66a372043b350cee870df46b81034473b70.tar.gz perlweeklychallenge-club-b145e66a372043b350cee870df46b81034473b70.tar.bz2 perlweeklychallenge-club-b145e66a372043b350cee870df46b81034473b70.zip | |
Ch146 (Clojure): Task 2
| -rw-r--r-- | challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj | 22 | ||||
| -rw-r--r-- | challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj | 11 |
2 files changed, 33 insertions, 0 deletions
diff --git a/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj b/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj new file mode 100644 index 0000000000..b259c8edc8 --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj @@ -0,0 +1,22 @@ +(ns c146.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +; Clojure supports Ratios (including as literals) +(def DEFAULT-INPUT [3/5]) + +(defn parent + [x] + (when (not= x 1) + (let [[n d] ((juxt numerator denominator) x)] + (if (< x 1) + (/ n (- d n)) ; left + (/ (- n d) d))))) ; right + +(defn -main + "Run Task 2 with a given input M, defaulting to the first example from the + task description." + [& args] + (let [[M] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (cl-format true "~{parent = '~a' and grandparent = '~a'~}~%" + (->> M (iterate parent) (drop 1) (take 2))))) diff --git a/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj b/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj new file mode 100644 index 0000000000..48dd9ca00a --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj @@ -0,0 +1,11 @@ +(ns c146.t2-test + (:refer-clojure :exclude [ancestors]) + (:require [clojure.test :refer [deftest is testing]] + [c146.t2 :refer [parent]])) + +(def ancestors (juxt parent (comp parent parent))) + +(deftest examples + (testing "Examples from task description" + (is (= [3/2 1/2] (ancestors 3/5))) + (is (= [1/3 1/2] (ancestors 4/3))))) |
