Changed clojure code to be more idiomatic. Uses pop instead of butlast and peek instead of last.

This commit is contained in:
Nils Norman Haukås 2014-11-17 13:22:25 +01:00
parent fa7cdeebec
commit ba23cfb76f

View file

@ -9,41 +9,43 @@
(doall (doall
(csv/read-csv in-file)))) (csv/read-csv in-file))))
(defn convertToNumeric [x] (defn convert-to-double [x]
(try (Double. x) (try (Double. x)
(catch Exception e 0))) (catch Exception e 0)))
(defn parseTime [entry] (defn parse-time [entry]
(let [custom-formatter (f/formatter "dd.MM.YYYY")] (let [custom-formatter (f/formatter "dd.MM.YYYY")]
(f/parse custom-formatter (nth entry 1)))) (f/parse custom-formatter (nth entry 1))))
(defn consecutiveDay [entry1 entry2] (defn consecutive-day? [entry1 entry2]
(or (empty? entry1) (or (empty? entry1)
(= (t/plus (parseTime entry1) (t/days 1)) (parseTime entry2)) (= (t/plus (parse-time entry1) (t/days 1)) (parse-time entry2))
(= (t/minus (parseTime entry1) (t/days 1)) (parseTime entry2)))) (= (t/minus (parse-time entry1) (t/days 1)) (parse-time entry2))))
(defn filterRain [place] (defn filter-rain [place]
(letfn [(getRainLevel [entry] (convertToNumeric (nth entry 5)))] (letfn [(get-rain-level [entry] (convert-to-double (nth entry 5)))]
(filter #(> (convertToNumeric (getRainLevel %)) 0) place))) (filter #(> (convert-to-double (get-rain-level %)) 0) place)))
(def rainObservationsByPlace (def rain-observations-by-place
(letfn [(location [entry] (nth entry 0))] (letfn [(location [entry] (nth entry 0))]
(partition-by location (filterRain (rest csv))))) (partition-by location (filter-rain (rest csv)))))
(defn findRainyPeriods (defn find-rainy-periods
"Builds nested list of rainy periods: [[[<-entry->]<-rainyperiods->]]" "Builds nested list of rainy periods: [[[<-entry->]<-rainyperiods->]]"
([remaining] (findRainyPeriods [[(first remaining)]] (rest remaining))) ([remaining] (find-rainy-periods [[(first remaining)]] (rest remaining)))
([result remaining] ([result remaining]
(if(empty? remaining) result (letfn [(add-to-lastperiod [result entry] (conj (pop result) (conj (peek result) entry)))
(if(consecutiveDay (last (last result)) (first remaining)) (create-new-period [result entry] (conj result [entry]))]
(recur (conj (vec (butlast result)) (conj (last result) (first remaining))) (rest remaining)) (if(empty? remaining) result
(recur (conj result [(first remaining)]) (rest remaining)))))) (if(consecutive-day? (peek (peek result)) (first remaining))
(recur (add-to-lastperiod result (first remaining)) (rest remaining))
(recur (create-new-period result (first remaining)) (rest remaining)))))))
(def rainyPeriods (map findRainyPeriods rainObservationsByPlace)) (def rainy-periods (map find-rainy-periods rain-observations-by-place))
(defn findRainiestPeriod [periods] (defn find-rainiest-period [periods]
(let [rainiestPeriods (map #(last (sort-by count %)) periods)] (let [rainiest-periods (map #(last (sort-by count %)) periods)]
(last (sort-by count rainiestPeriods)))) (last (sort-by count rainiest-periods))))
(findRainiestPeriod rainyPeriods) (find-rainiest-period rainy-periods)