diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2021-11-05 15:55:59 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2021-11-06 13:00:44 -0700 |
| commit | a3d286ab553679fdb1405ee8ffc7e4b8761823a6 (patch) | |
| tree | c21218bf7d1650da19cdf4b52d6551201e347ea2 | |
| parent | 4d91ca6d56ca464c41c704d14094ac4e7bf3b0d5 (diff) | |
| download | perlweeklychallenge-club-a3d286ab553679fdb1405ee8ffc7e4b8761823a6.tar.gz perlweeklychallenge-club-a3d286ab553679fdb1405ee8ffc7e4b8761823a6.tar.bz2 perlweeklychallenge-club-a3d286ab553679fdb1405ee8ffc7e4b8761823a6.zip | |
Ch137 (Clojure): Task 1
4 files changed, 62 insertions, 22 deletions
diff --git a/challenge-137/tyler-wardhaugh/clojure/bb.edn b/challenge-137/tyler-wardhaugh/clojure/bb.edn index 69331331eb..f0892047d3 100644 --- a/challenge-137/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-137/tyler-wardhaugh/clojure/bb.edn @@ -21,14 +21,16 @@ (-> file get-first-form second str))) (defn run-task - [task args] - (let [clj-options (format "-M -m %s " (get-task-ns task))] - (apply clojure clj-options args))) + ([task args] (run-task task args (get-task-ns task))) + ([task args task-ns] + (let [clj-options (format "-M -m %s " task-ns)] + (apply clojure clj-options args)))) (defn run-task-bb - [task args] - (let [bb-cmd (format "bb -m %s " (get-task-ns task))] - (apply shell bb-cmd args)))) + ([task args] (run-task-bb task args (get-task-ns task))) + ([task args task-ns] + (let [bb-cmd (format "bb -m %s " task-ns)] + (apply shell bb-cmd args))))) clean {:doc "Clean out temporary files" :task (run! fs/delete-tree @@ -61,10 +63,10 @@ c**** {:doc "CHALLENGE TASKS"} task-1 {:doc "Run Task 1 (via clojure)" - :task (run-task :t1 *command-line-args*)} + :task (run-task :t1 *command-line-args* "tw.weekly.c137.t1")} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (run-task-bb :t1 *command-line-args*)} + :task (run-task-bb :t1 *command-line-args* "tw.weekly.c137.t1")} task-2 {:doc "Run Task 2 (via clojure)" :task (run-task :t2 *command-line-args*)} diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj deleted file mode 100644 index 167c4a8269..0000000000 --- a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj +++ /dev/null @@ -1,12 +0,0 @@ -(ns tw.weekly.c137.t1 - (:require [clojure.edn :as edn])) - -;;; -; Task description for TASK #1 › Long Year -;;; - - -(defn -main - "Run Task 1 with a given input M and N, defaulting to the first example from - the task description." - [& args]) diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.cljc b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.cljc new file mode 100644 index 0000000000..a08e5444a9 --- /dev/null +++ b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.cljc @@ -0,0 +1,44 @@ +(ns tw.weekly.c137.t1 + (:require [clojure.pprint :refer [cl-format]]) + #?(:bb (:import []) + :clj (:import [java.time LocalDate] + [java.time.temporal WeekFields]))) +;;; +; Task description for TASK #1 › Long Year +;;; + +; define long-year-temporal? +#?(:bb + (defn long-year-temporal? [_] + (throw (Exception. "java.time.temporal not supported on Babashka"))) + + :clj + (let [week-of-year (.weekOfYear (WeekFields/ISO))] + (defn long-year-temporal? + "Is year long? (Uses Java's temporal library to get week number.)" + [year] + (-> (LocalDate/of year 12 28) + (.get week-of-year) + (= 53))))) + +; define long-year-manual? +; source: https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year +(let [p (fn [year] + (-> (+ year + (quot year 4) + (* -1 (quot year 100)) + (quot year 400)) + (mod 7)))] + (defn long-year-manual? + "Is year long? (Uses the method described in Wikipedia.)" + [year] + (or (= (p year) 4) (= (p (dec year)) 3)))) + +; choose an implementation based on our runtime (Clojure JVM or Babashka) +(def long-year? #?(:bb long-year-manual? :clj long-year-temporal?)) + +(defn -main + "Run Task 1 with a given input M and N, defaulting to the first example from + the task description." + [& _] + (cl-format true "~{~a~^, ~}~%" (filter long-year? (range 1900 2101)))) diff --git a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj index ddbab3e1c6..a083ce82f1 100644 --- a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj +++ b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj @@ -1,6 +1,12 @@ (ns tw.weekly.c137.t1-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c137.t1 :refer []])) + [tw.weekly.c137.t1 :refer [long-year-temporal? long-year-manual?]])) (deftest examples - (testing "Examples from description")) + (testing "Examples from description" + (let [source (range 1900 2101)] + (is (= (sequence (filter long-year-temporal?) source) + (sequence (filter long-year-manual?) source) + [1903 1908 1914 1920 1925 1931 1936 1942 1948 1953 1959 1964 1970 + 1976 1981 1987 1992 1998 2004 2009 2015 2020 2026 2032 2037 2043 + 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099]))))) |
